Repository: TheAlgorithms/Java
Pull request #3916 (2023-03-09 23:17:40)

Added the String Compression Algorithm #3911
<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


Swarga-codes: Should I remove the main function from the code and make a test file in the test folder for this particular algorithm?
Swarga-codes: @siriak  I made the necessary changes as u told on the PR, the changes include:

Reduce the size of code by dividing the code into smaller functions
Removed unnecessary comments
Removed the main
Included some Junit test cases for the code which you can access in the tests folder
Can you please check ?
Swarga-codes: I made the necessary changes as per your instructions, that is:
1) Changed the parameters for the checkCount function and passed the particular character rather than passing the entire array.
2) Changed the name of the resulting string "res" to "compressedString".
3) Changed the type of input from a character array to a string named "input".
4) Changed the reference link from Leetcode to Wikipedia.
5) Removed the unnecessary loop at the end as suggested.
6) Changed the name of checkCount function to a more descriptive name, that is, appendCount, since we are appending the count to the resulting string.
7) Changed the Junit test cases to parameterized Junit tests.
Can you please check?

Patch:
diff --git a/src/main/java/com/thealgorithms/strings/StringCompression.java b/src/main/java/com/thealgorithms/strings/StringCompression.java
new file mode 100644
index 00000000..0d209f88
--- /dev/null
+++ b/src/main/java/com/thealgorithms/strings/StringCompression.java
@@ -0,0 +1,60 @@
+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
+ * @author Swarga-codes (https://github.com/Swarga-codes)
+*/
+public class StringCompression {
+  /**
+   * Returns the compressed or encoded string
+   * 
+   * @param ch character array that contains the group of characters to be encoded
+   * @return the compressed character array as string
+   */
+  public static String compress(String input) {
+    // Keeping the count as 1 since every element present will have atleast a count
+    // of 1
+    int count = 1;
+    String compressedString = "";
+    // Base condition to check whether the array is of size 1, if it is then we
+    // return the array
+    if (input.length() == 1) {
+      return "" + input.charAt(0);
+    }
+    // If the array has a length greater than 1 we move into this loop
+    for (int i = 0; i < input.length() - 1; i++) {
+      // here we check for similarity of the adjacent elements and change the count
+      // accordingly
+      if (input.charAt(i) == input.charAt(i + 1)) {
+        count = count + 1;
+      }
+      if ((i + 1) == input.length() - 1 && input.charAt(i + 1) == input.charAt(i)) {
+        compressedString = appendCount(compressedString, count, input.charAt(i));
+        break;
+      } else if (input.charAt(i) != input.charAt(i+1)) {
+        if ((i + 1) == input.length() - 1) {
+          compressedString = appendCount(compressedString, count, input.charAt(i)) + input.charAt(i+1);
+          break;
+        } else {
+          compressedString = appendCount(compressedString, count, input.charAt(i));
+          count = 1;
+        }
+      }
+    }
+    return compressedString;
+  }
+  /**
+   * @param res   the resulting string
+   * @param count current count
+   * @param ch    the character at a particular index
+   * @return the res string appended with the count
+   */
+  public static String appendCount(String res, int count, char ch) {
+    if (count > 1) {
+      res += ch + "" + count;
+      count = 1;
+    } else {
+      res += ch + "";
+    }
+    return res;
+  }
+}
\ No newline at end of file


Test patch:
diff --git a/src/test/java/com/thealgorithms/strings/StringCompressionTest.java b/src/test/java/com/thealgorithms/strings/StringCompressionTest.java
new file mode 100644
index 00000000..d02e83ce
--- /dev/null
+++ b/src/test/java/com/thealgorithms/strings/StringCompressionTest.java
@@ -0,0 +1,13 @@
+package com.thealgorithms.strings;
+import static org.junit.jupiter.api.Assertions.*;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+public class StringCompressionTest {
+    @ParameterizedTest
+    @CsvSource({"a,a","aabbb,a2b3","abbbc,ab3c","aabccd,a2bc2d"})
+    void stringCompressionTest(String input,String expectedOutput){
+        String output=StringCompression.compress(input);
+        assertEquals(expectedOutput, output);
+    }
+}