Encode text with LZW · Compiler

Encode text with LZW

Encode the text with LZW. Start with dictionary entries A = 1 and B = 2. At the current position, emit the code of the longest dictionary entry that matches the next part of the text. If a symbol follows that phrase, add the phrase plus that symbol to the dictionary, using the next unused code starting at 3. Advance by the matched phrase's length. Stop after emitting the final phrase.

Input
One string of 1 to 100 symbols, each either uppercase A or uppercase B.
Output
Print the emitted integer codes in order on one line, separated by spaces.

Compiler Explorer runs your code when you run or check it.

Loading your exercise draft…