String Compression III
MedPrompt
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
2 approaches with complexity analysis and trade-offs.
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.
Walkthrough
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
Time
O(N^2), where N is the length of `word`. The `substring` operation is the bottleneck. In each iteration of the `while` loop, creating a substring takes time proportional to the length of the new string. In the worst-case scenario (e.g., a string with no repeating characters like 'abcde'), the loop runs N times, and the substring operations result in a quadratic time complexity.
Space
O(N^2) in the worst case. Although the final output string is O(N), the cumulative space for all intermediate substrings created during the loop can be quadratic. For an input of length N, strings of length approximately N-1, N-2, ..., 1 are created.
Trade-offs
Pros
Very straightforward to implement as it's a direct translation of the problem description.
Cons
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.
Solutions
Solution
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 walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.