Skip to content

#443 - String Compression

MediumArray / String
Open on LeetCode

Given an array of characters chars, compress it in-place using the following algorithm: for each group of consecutive repeating characters, append the character followed by the group's length (if length > 1). Return the new length of the array after compression. You must use only O(1) extra space.

InputOutput
chars = ["a","a","b","b","c","c","c"]6 | ["a","2","b","2","c","3"]
chars = ["a"]1 | ["a"]
chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]4 | ["a","b","1","2"]

Released under the MIT License.