Implement lossless dictionary compression for an input string whose words are separated by commas ,.
The encoding process must:
Provide these two functions:
encode(input_string) -> compressed_stringdecode(compressed_string) -> original_stringThe implementation must satisfy the following requirements:
decode may use only the value produced by encode; it must not depend on external or global state.s, decode(encode(s)) must reproduce s exactly.compressed_string = <dictionary_part> + ":" + <index_part>dictionary_part contains distinct words separated by commas, listed according to their first occurrence.index_part contains the corresponding 0-based dictionary index for every word in the original sequence, with indices separated by commas.Input:
"USA,USA,USA,USA,Mexico,Canada,Mexico,Mexico,Mexico"
input_string = USA, USA, USA, USA, Mexico, Canada, Mexico, Mexico, MexicoUSA,Mexico, Canada:0, 0, 0, 0, 1, 2, 1, 1, 1
The input is nine words separated by commas.
Encoded output:
"USA,Mexico,Canada:0,0,0,0,1,2,1,1,1"
This is produced because:
['USA', 'Mexico', 'Canada'].[0,0,0,0,1,2,1,1,1].1 <= number of words N <= 2 * 10^5; this includes inputs containing from 100,001 through 200,000 words., and :."" must also be handled.encode(input_string: str) -> strdecode(compressed_string: str) -> strinput_string = USA, USA, USA, USA, Mexico, Canada, Mexico, Mexico, MexicoUSA,Mexico, Canada:0, 0, 0, 0, 1, 2, 1, 1, 1
The input is nine words separated by commas.