Given an alphabet and a string, your job is to create the Lempel–Ziv–Welch compression of the string. Your implementation can either be a function with two parameters and a return value, or a full program that uses stdin and stdout.
The alphabet, in the form of a string, from which you will have to create the initial dictionary: Each character should be mapped to its index in the string, where the first index is 0. You will be expanding this dictionary as you iterate through the algorithm.
The string to be compressed.
- The compressed string in the form of dictionary keys. This must be an array of integers if you're creating a function, or printed integers separated by either whitespace or newline (and nothing else) if you're making a full program.
After you have initialized the dictionary, the algorithm goes something like this:
- Find the longest string W in the dictionary that matches the current input.
- Emit the dictionary index for W to output and remove W from the input.
- Add W followed by the next symbol in the input to the dictionary.
"ABC", "AABAACABBA" ==> [0,0,1,3,2,4,5] "be torn", "to be or not to be" ==> [3,4,2,0,1,2,4,5,2,6,4,3,2,7,9,1]
- No use of external resources
- No predefined built-in functions or libraries of any kind that solves the problem for you.