Compress a String Using Run-Length Encoding
Solve this Problems, compress it using run-length encodingRun-Length EncodingA simple compression scheme that replaces each run of consecutive identical characters with the character followed by how many times it repeats — "aaabbc" becomes "a3b2c".: every run of consecutive identical characters becomes that character followed by the run's length, unless the run has length 1, in which case just the character is written with no count.
Building the compressed result by repeatedly concatenating onto a plain string works, but in most languages a string is immutable — every += allocates a brand new string and copies everything built so far into it, turning n appends into O(n²) work. Appending to a mutable builder instead (a StringBuilder, a list joined at the end, or a pre-sized buffer) keeps each append O(1) amortized, bringing the whole pass down to O(n).
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
1 ≤ s.length ≤ 2000 - ◆
s consists only of lowercase English letters
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
| 1 | class Solution { |
| 2 | public String compressString(String s) { |
| 3 | StringBuilder result = new StringBuilder(); |
| 4 | int i = 0; |
| 5 | while (i < s.length()) { |
| 6 | char c = s.charAt(i); |
| 7 | int count = 0; |
| 8 | while (i < s.length() && s.charAt(i) == c) { |
| 9 | count++; |
| 10 | i++; |
| 11 | } |
| 12 | result.append(c); |
| 13 | if (count > 1) result.append(count); |
| 14 | } |
| 15 | return result.toString(); |
| 16 | } |
| 17 | } |
| 18 |
0Start with an empty (mutable) result builder and i at 0 — appending to it never rebuilds the whole string from scratch.
Approach & Solutions
Brute Force — Repeated Concatenation
BruteWalk through the string counting each run of repeated characters, and build the result by repeatedly concatenating onto a plain string with +=. Correct, but in languages with immutable strings every += copies everything built so far, making the whole pass O(n²) instead of O(n).
O(n²)O(n)1class Solution {
2 public String compressString(String s) {
3 String result = "";
4 int i = 0;
5 while (i < s.length()) {
6 char c = s.charAt(i);
7 int count = 0;
8 while (i < s.length() && s.charAt(i) == c) {
9 count++;
10 i++;
11 }
12 result += c;
13 if (count > 1) result += count;
14 }
15 return result;
16 }
17}Optimal — StringBuilder
OptimalIdentical run-counting logic, but append to a mutable builder (StringBuilder in Java, a list joined at the end in Python, a reserved std::string in C++, a pre-sized buffer in C) instead of rebuilding an immutable string on every append. Each append becomes O(1) amortized, so the whole pass is O(n).
O(n)O(n)1class Solution {
2 public String compressString(String s) {
3 StringBuilder result = new StringBuilder();
4 int i = 0;
5 while (i < s.length()) {
6 char c = s.charAt(i);
7 int count = 0;
8 while (i < s.length() && s.charAt(i) == c) {
9 count++;
10 i++;
11 }
12 result.append(c);
13 if (count > 1) result.append(count);
14 }
15 return result.toString();
16 }
17}