0

Given the following decoder, write the encoder. (The encoder should be written to compress whenever possible):

p14a8xkpq -> p14akkkkkkkkpq

(8xk gets decoded to kkkkkkkk. The only other requirement is that encodings be unambiguous)

Note that the String can have any possible ascii character

My approach would be to find sequences of repeating characters and replace them. For e.g. kkkkkkkk will b replaced by 8xk. However the problem with this solutin is that its ambigious. "8xk" may appear in the uncompressed string itself. I was thinking of using some special character to distinguish it, but then the string can have any possible character so that does not really help

user1628340
  • 901
  • 4
  • 14
  • 27
  • So you got a task. What would be steps in the right direction, what did you try, where are you stuck? – greybeard May 22 '16 at 21:40
  • updated the question with where I am stuck – user1628340 May 22 '16 at 22:39
  • (`updated the question with where I am stuck` A step in the right direction, as far as StackExchange is concerned. If you want a user to be notified of a comment to a comment, introduce her name with an `@`.) You have found yourself stuck "escaping" _repetition specifications_. What are variable parts thereof, is there a constant part? Can you replace that with a sequence that gets decoded to the original sequence? Once that is overcome, the specification might need a clarification: is replacing `kk` with `2xk` compression? You are welcome to answer your own question. – greybeard May 23 '16 at 06:48
  • You could begin with unit tests. And consider, 1xx -> x. Extra reading: Run-length encoding. – Tom Blodget May 24 '16 at 02:13

0 Answers0