Given an array of characters chars
, compress it using the following algorithm:
Begin with an empty string s
. For each group of consecutive repeating characters in chars
:
1
, append the character to s
.The compressed string s
should not be returned separately, but instead, be stored in the input character array chars
. Note that group lengths that are 10
or longer will be split into multiple characters in chars
.
After you are done modifying the input array, return the new length of the array.
You must write an algorithm that uses only constant extra space.
For example:
chars = ["a","a","b","b","c","c","c"]
should modify the array to ["a","2","b","2","c","3"]
and return 6.
chars = ["a"]
should return 1 and the array remains ["a"]
.
chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
should modify the array to ["a","b","1","2"]
and return 4.
A naive solution would involve creating a new string to store the compressed characters. We iterate through the input chars
array, count consecutive repeating characters, and then append the character and its count (if the count is greater than 1) to the new string. Finally, copy the compressed string back to the chars
array.
Code (Python):
def compress_naive(chars):
if not chars:
return 0
compressed = ""
count = 1
for i in range(len(chars)):
if i + 1 < len(chars) and chars[i] == chars[i + 1]:
count += 1
else:
compressed += chars[i]
if count > 1:
compressed += str(count)
count = 1
chars[:] = list(compressed)
return len(chars)
Big(O) Analysis:
compressed
string could have the same length as the input array.The optimal solution uses a two-pointer approach with constant extra space. We use one pointer (write_ptr
) to track the position where we write the compressed characters and another pointer (read_ptr
) to iterate through the input array.
Algorithm:
write_ptr
and read_ptr
to 0.chars
array using read_ptr
.chars[write_ptr]
and increment write_ptr
.chars
starting from write_ptr
.write_ptr
, which represents the new length of the compressed array.Code (Python):
def compress(chars):
write_ptr = 0
read_ptr = 0
while read_ptr < len(chars):
char = chars[read_ptr]
count = 0
while read_ptr < len(chars) and chars[read_ptr] == char:
read_ptr += 1
count += 1
chars[write_ptr] = char
write_ptr += 1
if count > 1:
for digit in str(count):
chars[write_ptr] = digit
write_ptr += 1
return write_ptr
Big(O) Analysis:
Edge Cases:
The optimal solution uses a two-pointer approach to compress the character array in-place with constant extra space. This approach is more efficient than the naive solution, which uses O(n) extra space.