String Compression III
MEDIUMDescription
Given a string word, compress it using the following algorithm:
- Begin with an empty string
comp. Whilewordis not empty, use the following operation:- Remove a maximum length prefix of
wordmade of a single charactercrepeating at most 9 times. - Append the length of the prefix followed by
ctocomp.
- Remove a maximum length prefix of
Return the string comp.
Example 1:
Input: word = "abcde"
Output: "1a1b1c1d1e"
Explanation:
Initially, comp = "". Apply the operation 5 times, choosing "a", "b", "c", "d", and "e" as the prefix in each operation.
For each prefix, append "1" followed by the character to comp.
Example 2:
Input: word = "aaaaaaaaaaaaaabb"
Output: "9a5a2b"
Explanation:
Initially, comp = "". Apply the operation 3 times, choosing "aaaaaaaaa", "aaaaa", and "bb" as the prefix in each operation.
- For prefix
"aaaaaaaaa", append"9"followed by"a"tocomp. - For prefix
"aaaaa", append"5"followed by"a"tocomp. - For prefix
"bb", append"2"followed by"b"tocomp.
Constraints:
1 <= word.length <= 2 * 105wordconsists only of lowercase English letters.
Approaches
Checkout 2 different approaches to solve String Compression III. Click on different approaches to view the approach and algorithm in detail.
Naive Simulation with Substrings
This approach directly implements the logic described in the problem statement. It works by repeatedly identifying the next chunk to compress, appending its compressed form to the result, and then creating a new, shorter string for the remaining part of the word. This process is repeated until the entire string is consumed.
Algorithm
- Initialize
compas an emptyStringBuilder. - Let
currentWordbe a copy of the inputword. - Loop while
currentWordis not empty:- Get the first character
c = currentWord.charAt(0). - Find
count, the length of the prefix ofcurrentWordconsisting only of characterc. - Determine the size of the chunk to process:
chunkSize = min(count, 9). - Append
chunkSizeandctocomp. - Update
currentWordby creating a new substring that excludes the processed chunk:currentWord = currentWord.substring(chunkSize).
- Get the first character
- Return the string representation of
comp.
This method simulates the compression process step-by-step as described. While conceptually simple because it's a literal translation of the problem, it suffers from significant performance issues due to the way strings are handled in many programming languages like Java, where they are immutable. The repeated creation of new substrings is a costly operation.
class Solution {
public String compressedString(String word) {
StringBuilder comp = new StringBuilder();
String currentWord = word;
while (!currentWord.isEmpty()) {
char c = currentWord.charAt(0);
int count = 0;
while (count < currentWord.length() && currentWord.charAt(count) == c) {
count++;
}
int chunkSize = Math.min(count, 9);
comp.append(chunkSize);
comp.append(c);
currentWord = currentWord.substring(chunkSize);
}
return comp.toString();
}
}
Complexity Analysis
Pros and Cons
- Very straightforward to implement as it's a direct translation of the problem description.
- Highly inefficient with a time complexity of O(N^2) in the worst case.
- Creates many temporary string objects, leading to high memory usage and garbage collector overhead.
- Likely to fail on large inputs with a 'Time Limit Exceeded' (TLE) error.
Code Solutions
Checking out 4 solutions in different languages for String Compression III. Click on different languages to view the code.
class Solution {
public
String compressedString(String word) {
StringBuilder ans = new StringBuilder();
int n = word.length();
for (int i = 0; i < n;) {
int j = i + 1;
while (j < n && word.charAt(j) == word.charAt(i)) {
++j;
}
int k = j - i;
while (k > 0) {
int x = Math.min(9, k);
ans.append(x).append(word.charAt(i));
k -= x;
}
i = j;
}
return ans.toString();
}
}
Video Solution
Watch the video walkthrough for String Compression III
Similar Questions
5 related questions you might find useful
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.