0% found this document useful (0 votes)
6 views3 pages

String Compression Java

The document explains a method for compressing strings in Java by replacing sequences of the same character with that character followed by the number of repetitions. It provides a step-by-step walkthrough of the algorithm, including a line-by-line code explanation and a complete Java implementation. The time complexity of the algorithm is O(n), while the space complexity is also O(n).

Uploaded by

ps66n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

String Compression Java

The document explains a method for compressing strings in Java by replacing sequences of the same character with that character followed by the number of repetitions. It provides a step-by-step walkthrough of the algorithm, including a line-by-line code explanation and a complete Java implementation. The time complexity of the algorithm is O(n), while the space complexity is also O(n).

Uploaded by

ps66n
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

String Compression in Java (Detailed Explanation)

Problem Statement

Compress a string by replacing sequences of the same character with that character followed by the number
of repetitions.

Example:
Input: "aaabbbcc"
Output: "a3b3c2"

Step-by-Step Walkthrough

Input: "aaabbbcc"

1. Start with first character 'a', count = 1


2. Next characters are 'a' -> count = 3
3. Next character is 'b' -> Append "a3" to result
4. Count 'b' -> count = 3 -> Append "b3"
5. Count 'c' -> count = 2 -> Append "c2"

Final result: "a3b3c2"

Line-by-Line Code Explanation

- if (str == null || str.length() == 0) return "";


-> Handle empty string input.

- String result = "";


-> To build and store the final result.

- char current = str.charAt(0);


-> Start with the first character.

- for (int i = 1; i < str.length(); i++)


String Compression in Java (Detailed Explanation)

-> Iterate through the string starting from the second character.

- if (str.charAt(i) == current) count++;


-> If same character, increase count.

- else { result += current + count; ... }


-> If different, add current + count to result and reset.

- After the loop, append the last group using:


result += current + count;

Java Code

public class StringCompressor {


public static String compressString(String str) {
if (str == null || str.length() == 0) return "";

String result = "";


int count = 1;
char current = str.charAt(0);

for (int i = 1; i < str.length(); i++) {


if (str.charAt(i) == current) {
count++;
} else {
result += current + String.valueOf(count);
current = str.charAt(i);
count = 1;
}
}

result += current + String.valueOf(count);


return result;
}

public static void main(String[] args) {


String input = "aaabbbcc";
String compressed = compressString(input);
System.out.println("Compressed string: " + compressed);
String Compression in Java (Detailed Explanation)

}
}

Output and Time Complexity

Output:
Compressed string: a3b3c2

Time Complexity: O(n)


- We scan the string once.
- Each group of repeating characters is counted and compressed.

Space Complexity: O(n)


- We store the result in a new string.

You might also like