Sort by Variable-Length Alphabet Tokens
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.