Quick 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.

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

Given an array of strings `words`, remove every string that is a prefix of any other string in the list, while keeping the original relative order of the remaining strings. A string `s` is removed if there exists a string `t` in `words` such that `t != s` and `t` starts with `s` (that is, `s` is a proper prefix of `t`). Identical copies of a string do not count as prefixes of each other, so duplicates are kept unless a longer string extends them. Return the array of remaining strings. ### Function Signature ```python def remove_prefix_strings(words: list[str]) -> list[str]: ``` ### Rules - The kept strings appear in the same relative order as in `words`. - Copies of the same string share one fate: every copy is kept, or every copy is removed because a longer string starts with it. ### Constraints - `1 <= len(words) <= 10^4` - `1 <= len(words[i]) <= 100` - `words[i]` consists of lowercase English letters. ### Examples ```text Input: words = ["apple", "app", "banana", "ban", "band", "cat"] Output: ["apple", "banana", "band", "cat"] ``` `"app"` is a prefix of `"apple"`, and `"ban"` is a prefix of `"banana"` and `"band"`, so both are removed. The rest keep their original order. ```text Input: words = ["a", "ab", "abc", "b"] Output: ["abc", "b"] ``` `"a"` and `"ab"` are prefixes of `"abc"`. `"abc"` and `"b"` are not prefixes of any other string. ```text Input: words = ["go", "go", "gone", "dog", "dog"] Output: ["gone", "dog", "dog"] ``` Both copies of `"go"` are removed because `"gone"` starts with `"go"`. The two copies of `"dog"` do not remove each other, and no longer string extends `"dog"`, so both stay.

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.

Given a list of strings `words`, remove every string that is a proper prefix of another string in the list, and return the strings that remain in their original relative order. A string `s` is removed if `words` contains a string `t` such that `t != s` and `t` starts with `s`. Identical copies of a string do not count as prefixes of each other, so copies of the same string share one fate: every copy is kept, or every copy is removed because a longer string in the list starts with it. Return the list of kept strings in the same relative order as they appear in `words`. Every copy of a kept duplicate appears in the output. Implement `remove_prefix_strings(words)`, which takes the list `words` and returns the list of kept strings. ### Example 1 ```text Input: words = ["apple", "app", "banana", "ban", "band", "cat"] Output: ["apple", "banana", "band", "cat"] ``` `"app"` is a prefix of `"apple"`, and `"ban"` is a prefix of both `"banana"` and `"band"`, so both are removed. The other strings keep their original order. ### Example 2 ```text Input: words = ["go", "go", "gone", "dog", "dog"] Output: ["gone", "dog", "dog"] ``` Both copies of `"go"` are removed because `"gone"` starts with `"go"`. The two copies of `"dog"` do not remove each other, and no longer string starts with `"dog"`, so both stay. ### Constraints - `1 <= len(words) <= 10^4` - `1 <= len(words[i]) <= 100` - `words[i]` consists of lowercase English letters.

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

  1. Copies of the same string share one fate, so the decision for a string depends only on which distinct strings are in the list.
  2. 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.
  3. 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.

Remove Strings That Prefix Another String

mediumRobloxSoftware Engineer

Given a list of strings words, remove every string that is a proper prefix of another string in the list, and return the strings that remain in their original relative order.

A string s is removed if words contains a string t such that t != s and t starts with s. Identical copies of a string do not count as prefixes of each other, so copies of the same string share one fate: every copy is kept, or every copy is removed because a longer string in the list starts with it.

Return the list of kept strings in the same relative order as they appear in words. Every copy of a kept duplicate appears in the output.

Implement remove_prefix_strings(words), which takes the list words and returns the list of kept strings.

Example 1

Input: words = ["apple", "app", "banana", "ban", "band", "cat"]
Output: ["apple", "banana", "band", "cat"]

"app" is a prefix of "apple", and "ban" is a prefix of both "banana" and "band", so both are removed. The other strings keep their original order.

Example 2

Input: words = ["go", "go", "gone", "dog", "dog"]
Output: ["gone", "dog", "dog"]

Both copies of "go" are removed because "gone" starts with "go". The two copies of "dog" do not remove each other, and no longer string starts with "dog", so both stay.

Constraints

  • 1 <= len(words) <= 10^4
  • 1 <= len(words[i]) <= 100
  • words[i] consists of lowercase English letters.

Examples

Example 1
Input
(['apple', 'app', 'banana', 'ban', 'band', 'cat'],)
Output
['apple', 'banana', 'band', 'cat']
Notes
Source example 1: 'app' is a prefix of 'apple' and 'ban' is a prefix of 'banana' and 'band', so both are removed.
Example 2
Input
(['a', 'ab', 'abc', 'b'],)
Output
['abc', 'b']
Notes
Source example 2: 'a' and 'ab' are prefixes of 'abc', and nothing longer starts with 'abc' or 'b'.

Constraints

  • •1 <= len(words) <= 10^4
  • •1 <= len(words[i]) <= 100
  • •words[i] consists of lowercase English letters.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def remove_prefix_strings(words: list[str]) -> list[str]:
Sample call
remove_prefix_strings(['apple', 'app', 'banana', 'ban', 'band', 'cat'])
Language
Case 1
Input
(['apple', 'app', 'banana', 'ban', 'band', 'cat'],)
Expected
['apple', 'banana', 'band', 'cat']
Case 2
Input
(['a', 'ab', 'abc', 'b'],)
Expected
['abc', 'b']

Browse More Questions

Show the approach

Approach

Sort the distinct strings of words in lexicographic order. If a string s is a proper prefix of some string t in the list, then the distinct string that comes right after s in this order also starts with s. To see this, call that next string u, so s < u <= t. If u did not start with s, it could not be a prefix of s either (that would make u < s), so u and s first differ at some index i < len(s) with u[i] > s[i]. Because t matches s through index i, this gives u > t, a contradiction. One pass over neighboring pairs of the sorted distinct list therefore finds every removable string: mark sorted[i] whenever sorted[i + 1] starts with it. Neighbors in the distinct list are always different strings, so every match is a proper prefix and identical copies never remove each other. A final pass over words in its original order keeps each string that is not marked, which preserves relative order and keeps or removes all copies of a duplicate together. Edge cases: a single word is always kept; a list made only of copies of one string comes back unchanged; a shared stem such as abc and abd removes neither; a string found inside another string away from its start, such as pp in apple, is kept. The output holds only strings, so no integer-width concerns arise.

Time complexity:
O(N * L * log N), where N is len(words) and L is the maximum word length
Space complexity:
O(N * L)