DEV Community

Discussion on: String Compression. Facebook interview question.

Collapse
akhilpokle profile image
Akhil Author

Yep, the function can only take string with characters as a input for encoding, with integers it becomes quite complicated but doable by using some special characters like "$" or "#" and it must be guaranteed that the said special character won't be used.

Your method is actually a much smarter way of achieving it! Thanks for the tip :)

Collapse
hanpari profile image
Pavel Morava • Edited on

Thank you.

I have mixed feelings when being complimented over this code since I've abused loose type coercion of Javascript when joining characters and numbers, not to mention the nasty xs[xs.Length-1]++ thing; both should be used as an example of how dangerous the language is.

As for your first paragraph, there is no need for any guarantees. In such cases, some escape sequence is typically used. It is similar to "one line \n second line" vs "one line \\n no second line". You just have to double your special character.