DEV Community

Discussion on: String Compression

Collapse
 
grodzickir profile image
Ryszard Grodzicki

Simple improvement proposal: you could remove the last check by replacing chars only if there are more than 2 in a row.
Replacing 'CC' with 'C2' never saves space. Replacing more than 2 characters always saves space. So you would either return the same input string or return compressed string that's shorter than original.

Collapse
 
codewithml profile image
CodeWithML

I could do something like if count is more than 2 than add the character with count, if count is 1, than only add the character , and lastly if count is exactly 2, then add the character 2 times, so it would remain same as original string.