@@ -0,0 +1,1917 @@
+package com.rampatra.strings;%0A%0A/**%0A * @author rampatra%0A * @since 2019-04-08%0A */%0Apublic class CompressString %7B%0A%0A    /**%0A     * Compress a string, consisting of only alphabets, such that the compressed string contains the letter and%0A     * a number next to it representing the number of times it is repeated. Also, compress only if the letter is%0A     * repeated more than once and ignore it occurs just once.%0A     * EXAMPLE:%0A     * Input: aaabbcdd%0A     * Output: a3b2cd2%0A     * %3Cp%3E%0A     * Time Complexity: O(n)%0A     * Space Complexity: O(n)%0A     * where,%0A     * n is the number of characters in the input string%0A     *%0A     * @param str the input string consisting on only alphabets%0A     * @return the compressed string%0A     */%0A    private static String compress(String str) %7B%0A        // some basic validation%0A        if (str.length() == 0) %7B%0A            throw new IllegalArgumentException(%22Empty String%22);%0A        %7D%0A        %0A        StringBuilder sb = new StringBuilder();%0A        int letterCount = 0;%0A%0A        for (int i = 0; i %3C str.length(); i++) %7B%0A            /*%0A                When the current character is a different one, append the previous character and its count to the%0A                result, and finally, reset the counter%0A             */%0A            if (i != 0 && str.charAt(i) != str.charAt(i - 1)) %7B%0A                sb.append(str.charAt(i - 1));%0A                if (letterCount %3E 1) sb.append(letterCount);%0A                letterCount = 0;%0A            %7D%0A            letterCount++;%0A        %7D%0A%0A        // last character%0A        sb.append(str.charAt(str.length() - 1));%0A        if (letterCount %3E 1) sb.append(letterCount);%0A%0A        return sb.toString();%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        System.out.println(compress(%22a%22));%0A        System.out.println(compress(%22aabbcc%22));%0A        System.out.println(compress(%22aabcc%22));%0A        System.out.println(compress(%22aaaabbbccaad%22));%0A    %7D%0A%7D

Compress string: done