Skip to content

Commit e3ad376

Browse files
alxkmalxkm
and
alxkm
authored
refactor: StringCompression (#5410)
refactor: StringCompression Co-authored-by: alxkm <[email protected]>
1 parent af7c425 commit e3ad376

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

src/main/java/com/thealgorithms/strings/StringCompression.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.thealgorithms.strings;
2-
/* References : https://en.wikipedia.org/wiki/Run-length_encoding
3-
* String compression algorithm deals with encoding the string, that is, shortening the size of the
4-
* string
2+
3+
/**
4+
* References : https://en.wikipedia.org/wiki/Run-length_encoding
5+
* String compression algorithm deals with encoding the string, that is, shortening the size of the string
56
* @author Swarga-codes (https://github.com/Swarga-codes)
67
*/
78
public final class StringCompression {
@@ -14,19 +15,16 @@ private StringCompression() {
1415
* @return the compressed character array as string
1516
*/
1617
public static String compress(String input) {
17-
// Keeping the count as 1 since every element present will have atleast a count
18-
// of 1
18+
// Keeping the count as 1 since every element present will have at least a count of 1
1919
int count = 1;
2020
String compressedString = "";
21-
// Base condition to check whether the array is of size 1, if it is then we
22-
// return the array
21+
// Base condition to check whether the array is of size 1, if it is then we return the array
2322
if (input.length() == 1) {
2423
return "" + input.charAt(0);
2524
}
2625
// If the array has a length greater than 1 we move into this loop
2726
for (int i = 0; i < input.length() - 1; i++) {
28-
// here we check for similarity of the adjacent elements and change the count
29-
// accordingly
27+
// here we check for similarity of the adjacent elements and change the count accordingly
3028
if (input.charAt(i) == input.charAt(i + 1)) {
3129
count = count + 1;
3230
}
@@ -54,7 +52,6 @@ public static String compress(String input) {
5452
public static String appendCount(String res, int count, char ch) {
5553
if (count > 1) {
5654
res += ch + "" + count;
57-
count = 1;
5855
} else {
5956
res += ch + "";
6057
}

src/test/java/com/thealgorithms/strings/StringCompressionTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.thealgorithms.strings;
2+
23
import static org.junit.jupiter.api.Assertions.assertEquals;
34

45
import org.junit.jupiter.params.ParameterizedTest;
56
import org.junit.jupiter.params.provider.CsvSource;
67

78
public class StringCompressionTest {
89
@ParameterizedTest
9-
@CsvSource({"a,a", "aabbb,a2b3", "abbbc,ab3c", "aabccd,a2bc2d"})
10+
@CsvSource({"'a', 'a'", "'aabbb', 'a2b3'", "'abbbc', 'ab3c'", "'aabccd', 'a2bc2d'", "'aaaabbbcccc', 'a4b3c4'", "'abcd', 'abcd'", "'aabbccdd', 'a2b2c2d2'", "'aaabbaa', 'a3b2a2'", "'', ''", "'a', 'a'", "'aaaaa', 'a5'", "'aabb', 'a2b2'", "'aabbbaaa', 'a2b3a3'", "'qwerty', 'qwerty'"})
1011
void stringCompressionTest(String input, String expectedOutput) {
1112
String output = StringCompression.compress(input);
1213
assertEquals(expectedOutput, output);

0 commit comments

Comments
 (0)