Problem · String
Remove Prefix Strings
Learn this problemProblem statement
You are given an array of strings words. Remove words[i] if it is a prefix of any strictly longer string in the array.
Return the strings that remain in their original input order. Equal strings do not remove one another merely because they match; however, every copy of a string is removed if that string is a prefix of some longer word.
Function
removePrefixStrings(words: String[]) → String[]Examples
Example 1
words = ["ab", "abc", "abcd", "bc", "bcd", "bd"]return = ["abcd", "bcd", "bd"]ab and abc prefix longer strings, and bc prefixes bcd, so those three strings are removed.
Example 2
words = ["a", "ab", "abc"]return = ["abc"]a and ab are prefixes of longer strings. Only abc remains.
Example 3
words = ["apple", "banana", "cherry"]return = ["apple", "banana", "cherry"]No word is a prefix of a longer word in the input.
Example 4
words = ["a", "a", "ab"]return = ["ab"]Both copies of a are prefixes of the longer word ab, so both are removed.
Constraints
1 <= words.length <= 100001 <= words[i].length <= 100words[i]contains lowercase English letters.- Preserve the original order of the strings that remain.
- Only a strictly longer string can cause removal.
More Roblox problems
- Most Frequent Call Stack Per ThreadPHONE SCREEN · Seen Jul 2026
- Break a PalindromeOA · Seen Jun 2026
- Candy Crush Grid Matching and GravityPHONE SCREEN · Seen Jun 2026
- Closest Binary Search Tree Value — Base PracticePHONE SCREEN · Seen Jun 2026
- Design Search Autocomplete SystemPHONE SCREEN · Seen Jun 2026
- Grid Pathfinding with Obstacles (DFS)PHONE SCREEN · Seen Jun 2026
- Maximize Distance to Closest Person — Return the SeatONSITE INTERVIEW · Seen Jun 2026
- Maximum Number of Balls in a BoxPHONE SCREEN · Seen Jun 2026