FastPrepSort by Variable-Length Alphabet Tokens

Sort by Variable-Length Alphabet Tokens

Bloomberg LP logoBloomberg LP● HardNEW GRADPHONE SCREEN
Learn

Problem statement

alphabet lists language symbols from smallest to largest; each symbol is a nonempty string and lengths may vary. Every input word has exactly one tokenization into these symbols.

Sort words lexicographically by their token-rank sequences. If one sequence is a prefix of another, the shorter word comes first.

Function

sortTokenAlphabetWords(alphabet: String[], words: String[]) → String[]

Examples

Example 1

alphabet = ["ba","aa","cb","abc","d","dd"]words = ["cbba","abccb","aaba","baaa"]return = ["baaa","aaba","cbba","abccb"]

The unique token sequences begin with ranks 0,1,2,3 respectively.

Constraints

  • Every word is uniquely tokenizable.
  • Total input length is at most 10^5.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public String[] sortTokenAlphabetWords(String[] alphabet, String[] words) {
  // Write your code here.
}
alphabet["ba","aa","cb","abc","d","dd"]
words["cbba","abccb","aaba","baaa"]
expected["baaa", "aaba", "cbba", "abccb"]
Checking account…