diff --git a/src/main/java/com/ctci/arraysandstrings/StringCompressionTest.java b/src/main/java/com/ctci/arraysandstrings/StringCompressionTest.java new file mode 100644 index 00000000..291a25a6 --- /dev/null +++ b/src/main/java/com/ctci/arraysandstrings/StringCompressionTest.java @@ -0,0 +1,66 @@ +package com.ctci.arraysandstrings; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class StringCompressionTest { + @Test + void case1() { + String input = ""; + String expectedOutput = ""; + String actualOutput = compressString(input); + assertEquals(expectedOutput, actualOutput); + } + + @Test + void case2() { + String input = "abc"; + String expectedOutput = "abc"; + String actualOutput = compressString(input); + assertEquals(expectedOutput, actualOutput); + } + + @Test + void case3() { + String input = "aaabbccc"; + String expectedOutput = "a3b2c3"; + String actualOutput = compressString(input); + assertEquals(expectedOutput, actualOutput); + } + + @Test + void case4() { + String input = "aaabbbccc"; + String expectedOutput = "a3b3c3"; + String actualOutput = compressString(input); + assertEquals(expectedOutput, actualOutput); + } + + @Test + void case5() { + String input = "abcd"; + String expectedOutput = "abcd"; + String actualOutput = compressString(input); + assertEquals(expectedOutput, actualOutput); + } + + + private static String compressString(String str) { + StringBuilder compressedSb = new StringBuilder(); + int countConsecutive = 0; + for (int i = 0; i < str.length(); i++) { + countConsecutive++; + + /* If next character is different than current, append this char to result. */ + if (i + 1 >= str.length() || str.charAt(i) != str.charAt(i + 1)) { + compressedSb.append(str.charAt(i)); + compressedSb.append(countConsecutive); + countConsecutive = 0; + } + } + return compressedSb.length() < str.length() ? compressedSb.toString() : str; + } +} \ No newline at end of file pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy