Loading...
Loading...
You are given a list of strings words and an integer maxVocabSize. Each string in words is a word of lowercase English letters.
Token IDs 0 to 25 stand for the letters 'a' to 'z', so a word starts out as the list of IDs of its letters. The string spelled by a pair of neighboring tokens is the letters of those two tokens written one after the other.
Consider an operation where you count every pair of neighboring tokens inside every word. Tokens at the end of one word and at the start of another are never neighbors. The pair with the largest count gets the next free token ID, starting at 26. If several pairs tie for the largest count, the pair that spells the lexicographically smallest string is chosen. Then, in each word, you scan from left to right and replace every two neighboring tokens that form the chosen pair with the new token, moving past the new token before continuing.
Given that you repeat this operation until no pairs are left or until the next free token ID would be maxVocabSize, return the token IDs of every word in words, keeping the order of words.
1 <= words.length <= 50001 <= words[i].length <= 10words[i] consists of lowercase English letters only.words contains at most 500 distinct strings.26 <= maxVocabSize <= 800The word has one neighboring pair, aa, so it takes token 26 and the word is encoded as [26].
ab occurs twice and ba once, so ab becomes token 26. The limit 27 stops the process right after that, so ba stays as [1,0].
Both pairs occur once, so the tie goes to ab, the smaller string. Then bc is the only pair left and takes 27.
There are no additional token IDs left after the lowercase letters, so we simply replace each character with its token ID.