Gamified challenge · Hash maps
Split every word in words into single letters (each list entry counts separately, so repeats count more). Then make up to k merges: count every adjacent pair of symbols in every word (overlaps count: aaa has the pair a,a twice), pick the most frequent pair, and replace each occurrence in every word, scanning left to right without overlaps, by one symbol glued together.
Ties go to the smallest pair: compare the left symbols as strings, then the right symbols ("a" < "ab" < "b"). Stop early if no word has two symbols left. Write bpeMerges(words, k) returning the merges in order, each written left+right.
Example: ["banana", "bandana", "ban", "nab"], k = 4 gives ["a+n", "b+an", "an+a", "a+b"]: a,n appears 5 times, then b,an 3 times, then an,a twice; in the last round every pair appears once, and a,b has the smallest left symbol.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google