String Compression
String, Iterative approach
Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa
would become a2blc5a3
. If the "compressed" string would not become smaller than the original string, your method should return the original string. You can assume the string has only uppercase and lowercase letters (a - z).
Solutions
Approach - Copy characters to new String
At first glance, implementing this method seems fairly straightforward, but perhaps a bit tedious. We iterate through the string, copying characters to a new string and counting the repeats. At each iteration, check if the current character is the same as the next character. If not, add its compressed version to the result.
Steps
First check if string is null, then return string.
Create a new empty string to store the characters.
Create character counter variable.
Run loop over the length of string and increment character count. Check if current character is not same a next one then, add current character into result string and also check if current string counter value is greater than one, then add counter value also into result string. Now, reset counter to zero.
Return new string.
Complexity
Time Complexity: O(n + k²)
Auxiliary Space: O(n)
The runtime is O(n + k²), where n
is the size of the original string and k
is the number of character sequences. For example, if the string is aabccdeeaa
, then there are six charactes sequences. It's slow because string concatenation operates in O(n²) time. We can fix this by using a StringBuilder
.
Approach - Copy characters to StringBuilder
Steps
First check if string is null, then return string.
Create a new StringBuilder instance to store the characters.
Create character counter variable.
Run loop over the length of string and increment character count. Check if current character is not same a next one then, add current character into result StringBuilder and also check if current string counter value is greater than one, then add counter value also into result StringBuilder. Now, reset counter to zero.
Return new string.
Complexity
Time Complexity: O(n)
Auxiliary Space: O(n)
Last updated