String Compression in Place
Given a list of characters, compress it in place: each run becomes the character followed by the digits of its length, with the length omitted when the run is a single character. Return the new length; the first that many entries of the list hold the result. Use O(1) extra space.
["a","a","b","b","b","c"] -> 5, list starts ["a","2","b","3","c"] ["z"] * 12 -> 3, list starts ["z","1","2"]
Hints
Hint 1
Keep a read pointer that finds the end of each run and a write pointer where the compressed output goes.
Hint 2
The compressed form of a run is never longer than the run itself, so the write pointer can never overtake the read pointer.
Topics
Asked at
Sign in to use the assistant — ask what the problem means, get a hint when you're stuck, or have your current work reviewed. Conversations save to your account.