Remove Strings That Are Proper Prefixes of Other Strings in the List
Company: Roblox
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
Overview: Given a list of lowercase words, remove each word that is a proper prefix of another word in the list while keeping the original order and duplicate copies. It tests fast prefix lookups over up to 10,000 strings and careful handling of identical strings.
Constraints
- 1 <= len(words) <= 10^4
- 1 <= len(words[i]) <= 100
- words[i] consists of lowercase English letters.
Examples
Input: (['apple', 'app', 'banana', 'ban', 'band', 'cat'],)
Expected Output: ['apple', 'banana', 'band', 'cat']
Explanation: Source example 1: 'app' is a prefix of 'apple' and 'ban' is a prefix of 'banana' and 'band', so both are removed.
Input: (['a', 'ab', 'abc', 'b'],)
Expected Output: ['abc', 'b']
Explanation: Source example 2: 'a' and 'ab' are prefixes of 'abc', and nothing longer starts with 'abc' or 'b'.
Hints
- Copies of the same string share one fate, so the decision for a string depends only on which distinct strings are in the list.
- A string is removed only when some different string starts with it; an occurrence in the middle or at the end of another string does not count.
- The output keeps the original order, so you can decide which strings to remove first and then make one pass over words to build the answer.