refactor: StringCompression
(#5410)
refactor: StringCompression Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
parent
af7c425010
commit
e3ad3761fd
@ -1,7 +1,8 @@
|
|||||||
package com.thealgorithms.strings;
|
package com.thealgorithms.strings;
|
||||||
/* References : https://en.wikipedia.org/wiki/Run-length_encoding
|
|
||||||
* String compression algorithm deals with encoding the string, that is, shortening the size of the
|
/**
|
||||||
* string
|
* References : https://en.wikipedia.org/wiki/Run-length_encoding
|
||||||
|
* String compression algorithm deals with encoding the string, that is, shortening the size of the string
|
||||||
* @author Swarga-codes (https://github.com/Swarga-codes)
|
* @author Swarga-codes (https://github.com/Swarga-codes)
|
||||||
*/
|
*/
|
||||||
public final class StringCompression {
|
public final class StringCompression {
|
||||||
@ -14,19 +15,16 @@ public final class StringCompression {
|
|||||||
* @return the compressed character array as string
|
* @return the compressed character array as string
|
||||||
*/
|
*/
|
||||||
public static String compress(String input) {
|
public static String compress(String input) {
|
||||||
// Keeping the count as 1 since every element present will have atleast a count
|
// Keeping the count as 1 since every element present will have at least a count of 1
|
||||||
// of 1
|
|
||||||
int count = 1;
|
int count = 1;
|
||||||
String compressedString = "";
|
String compressedString = "";
|
||||||
// Base condition to check whether the array is of size 1, if it is then we
|
// Base condition to check whether the array is of size 1, if it is then we return the array
|
||||||
// return the array
|
|
||||||
if (input.length() == 1) {
|
if (input.length() == 1) {
|
||||||
return "" + input.charAt(0);
|
return "" + input.charAt(0);
|
||||||
}
|
}
|
||||||
// If the array has a length greater than 1 we move into this loop
|
// If the array has a length greater than 1 we move into this loop
|
||||||
for (int i = 0; i < input.length() - 1; i++) {
|
for (int i = 0; i < input.length() - 1; i++) {
|
||||||
// here we check for similarity of the adjacent elements and change the count
|
// here we check for similarity of the adjacent elements and change the count accordingly
|
||||||
// accordingly
|
|
||||||
if (input.charAt(i) == input.charAt(i + 1)) {
|
if (input.charAt(i) == input.charAt(i + 1)) {
|
||||||
count = count + 1;
|
count = count + 1;
|
||||||
}
|
}
|
||||||
@ -54,7 +52,6 @@ public final class StringCompression {
|
|||||||
public static String appendCount(String res, int count, char ch) {
|
public static String appendCount(String res, int count, char ch) {
|
||||||
if (count > 1) {
|
if (count > 1) {
|
||||||
res += ch + "" + count;
|
res += ch + "" + count;
|
||||||
count = 1;
|
|
||||||
} else {
|
} else {
|
||||||
res += ch + "";
|
res += ch + "";
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
package com.thealgorithms.strings;
|
package com.thealgorithms.strings;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
@ -6,7 +7,7 @@ import org.junit.jupiter.params.provider.CsvSource;
|
|||||||
|
|
||||||
public class StringCompressionTest {
|
public class StringCompressionTest {
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@CsvSource({"a,a", "aabbb,a2b3", "abbbc,ab3c", "aabccd,a2bc2d"})
|
@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'"})
|
||||||
void stringCompressionTest(String input, String expectedOutput) {
|
void stringCompressionTest(String input, String expectedOutput) {
|
||||||
String output = StringCompression.compress(input);
|
String output = StringCompression.compress(input);
|
||||||
assertEquals(expectedOutput, output);
|
assertEquals(expectedOutput, output);
|
||||||
|
Loading…
Reference in New Issue
Block a user