Problem 418350 · easy · Level 04 Non-Linear Data Structures

The Stem Every Part Code Shares

trie · strings

A parts catalogue groups items whose codes begin the same way. Given the codes words, return the longest string that every code starts with. If the codes do not even share their first character (or the list is empty), return "".

Examples

Input:  words = ["planet", "plane", "plaza"]
Output: "pla"

Input:  words = ["kiwi", "lime", "mango"]
Output: ""

Input:  words = ["same", "same"]
Output: "same"

Constraints

  • 0 <= len(words) <= 10**4, each word has length <= 1000
  • Words are lowercase letters (a word may be empty)
  • Target: O(total characters)

Goals

  • Recognise the common prefix as the single-child chain at the top of a trie
  • Handle an empty list and an empty word correctly
Starting Python…