I have a long string for example it could be "aaaaaabbccc". Need to represent it as "a6b2c3". What's the best way to do this ? I could do this in linear time by comparing characters and incrementing counts and then replacing the counts in the array, using two indexes in one pass. Can you guys think of a better way than this? Are any of the encoding techniques going to work here ?
+3
A:
The common solution for this is RLE - Run-length encoding, the Wikipedia article has sample implementation code.
Carlos Gutiérrez
2010-03-09 01:17:33
A:
I think you're asking, "Is there a better than linear way to do run-length encoding"? If so, the answer is no.
Matthew Flaschen
2010-03-09 01:18:37
+1
A:
I don't think there is a faster way to solve it.
Informally you can think that a sub-linear complexity implies to do less comparisons that the number of the characters in the string you want to compress. But with a number of comparisons such small you can't be sure of some character, you can't know what they contain because you don't have enough information.. this means that you can't obtain a loseless compression.
Jack
2010-03-09 01:24:27