String Compression III - Complete Solution Guide
String Compression III is LeetCode problem 3163, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a string word , compress it using the following algorithm: Begin with an empty string comp . While word is not empty, use the following operation: Remove a maximum length prefix of word made of a single character c repeating at most 9 times. Append the length of the prefix followed by c to comp . 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 pref
Detailed Explanation
The problem requires us to compress a given string 'word' by replacing consecutive repeating characters with the count of their repetitions followed by the character itself. The repetitions must be handled in chunks of at most 9 consecutive characters. The compressed string 'comp' is built iteratively from left to right by identifying the maximum-length prefix composed of a single character repeated at most 9 times, appending the length of the prefix and the character to 'comp', and repeating the process until 'word' is empty. For example, 'aaaaaaaaaaaaaabb' becomes '9a5a2b' because there are twelve 'a's initially which are split into a '9a' and a '5a', followed by '2b'.
Solution Approach
The provided solution uses a simple iterative approach. It iterates through the input string, tracking the current character and its count. When the character changes or the count reaches 9, it appends the count and the character to the result string. The process repeats until the entire input string has been processed.
Step-by-Step Algorithm
- Step 1: Initialize an empty result string (or a list of characters in Python for better performance during appending).
- Step 2: Initialize an index 'i' to 0 to traverse the input string.
- Step 3: While 'i' is less than the length of the input string, repeat steps 4-7.
- Step 4: Identify the character at the current index 'i'.
- Step 5: Initialize a counter 'count' to 0 and another index 'j' to 'i'.
- Step 6: Increment 'j' as long as 'j' is within the bounds of the string, the character at 'j' is the same as the identified character, and the count is less than 9.
- Step 7: Append the 'count' (converted to string) and the identified character to the result string.
- Step 8: Update 'i' to the current value of 'j', effectively skipping the processed prefix.
- Step 9: Once the loop finishes, convert the result list to a string (if using Python list) and return the string.
Key Insights
- Insight 1: The core idea is to iterate through the input string and identify consecutive segments of the same character.
- Insight 2: We need to limit the length of each segment to a maximum of 9, as only single-digit counts are allowed in the compressed string.
- Insight 3: Efficient string manipulation and concatenation are crucial for optimal performance.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: String.
Companies
Asked at: Affirm, Qualcomm.