Given an array of characters chars
, compress it in-place using the following algorithm:
s
.chars
:
1
, append the character to s
.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.
Example 1:
Input: chars = ["a","a","b","b","c","c","c"]
Output: Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]
Explanation: The groups are "aa", "bb", and "ccc". This compresses to "a2b2c3".
Example 2:
Input: chars = ["a"]
Output: Return 1, and the first character of the input array should be: ["a"]
Explanation: The only group is "a", which remains uncompressed since it's a single character.
Example 3:
Input: chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
Output: Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].
Explanation: The groups are "a" and "bbbbbbbbbbbb". This compresses to "ab12".
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for string compression involves checking every possible combination. We explore all potential compressed versions and pick the shortest one. It's like trying every possibility, no matter how inefficient.
Here's how the algorithm would work step-by-step:
def string_compression_brute_force(input_string):
shortest_compressed_string = input_string
string_length = len(input_string)
for sub_string_length in range(1, string_length // 2 + 1):
for start_index in range(string_length - sub_string_length + 1):
sub_string = input_string[start_index:start_index + sub_string_length]
repeat_count = 1
current_index = start_index + sub_string_length
# Count how many times the substring repeats
while current_index + sub_string_length <= string_length and \
input_string[current_index:current_index + sub_string_length] == sub_string:
repeat_count += 1
current_index += sub_string_length
if repeat_count > 1:
# Attempt to compress with the repeated sequence.
compressed_string = input_string[:start_index] + str(repeat_count) + sub_string + input_string[current_index:]
#Need to compare against existing shortest string
if len(compressed_string) < len(shortest_compressed_string):
shortest_compressed_string = compressed_string
return shortest_compressed_string
The goal is to shorten a string by replacing repeating characters with the character and the number of times it repeats. We can accomplish this efficiently by walking through the string once, keeping track of the current character and how many times we've seen it.
Here's how the algorithm would work step-by-step:
def string_compression(input_string):
compressed_string = ""
string_length = len(input_string)
if string_length == 0:
return ""
current_character = input_string[0]
character_count = 1
for i in range(1, string_length):
if input_string[i] == current_character:
character_count += 1
else:
# Append character/count to result
compressed_string += current_character
if character_count > 1:
compressed_string += str(character_count)
current_character = input_string[i]
character_count = 1
# Append the last character sequence
compressed_string += current_character
if character_count > 1:
compressed_string += str(character_count)
return compressed_string
Case | How to Handle |
---|---|
Empty input array | Return 0 as the new length for an empty array. |
Input array with a single character | Return 1 as the new length since no compression is needed. |
Input array with all identical characters | Compress to character followed by the total count; ensure correct digit-by-digit insertion for large counts. |
Input array with no repeating characters | The array remains unchanged and the length remains the same. |
Input array with repeating characters at the beginning and end, but not in the middle | Handle each group of repeating characters independently and update the index correctly. |
Count exceeding a single digit (e.g., count of 10 or more) | Convert the count to individual digits and insert them into the char array one by one. |
Maximum possible length of the input array | The solution should scale linearly with the input size, ensuring no excessive memory usage or stack overflow. |
Array with repeating characters where the compressed version takes more space than the original | Ensure that the compressed character count is being handled by overwriting what is already present in the original array. |