Shared Start of Product Codes
Easy · Tries & String Matching · about 10 min
A warehouse wants to group product codes by their shared beginning. Given a list of codes, return the longest string that every code starts with.
Return "" if the list is empty or the codes share no first letter. Codes may contain any printable characters and may be empty.
Examples:
shared_code_start(["SKU-1042", "SKU-1099", "SKU-10"]) → "SKU-10"
shared_code_start(["abc", "xyz"]) → ""
Hints
- Start with the whole first code as your answer.
- Compare it with each other code letter by letter and cut it at the first place they differ (or where either one ends).
- Once the answer is empty you can stop early.
Learn the pattern first: the Tries & String Matching lesson.
More Tries & String Matching problems
- Search Box Counter (Easy)
- Shorten Words to Their Stems (Medium)
- Dictionary Words on a Letter Board (Hard)
- Signal in a Ring Buffer (Hard)