Problem · String

Remove Prefix Strings

Learn this problem
MediumRoblox logoRobloxFULLTIMEPHONE SCREEN
See Roblox hiring insights

Problem 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 <= 10000
  • 1 <= words[i].length <= 100
  • words[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

drafts saved locally
public String[] removePrefixStrings(String[] words) {
  // write your code here
}
words["ab", "abc", "abcd", "bc", "bcd", "bd"]
expected["abcd", "bcd", "bd"]
checking account