public static String compressString(String str){
        StringBuilder compressedStr = new StringBuilder();
        int i = 0;
        while(i < str.length()){
            int count = 1;
            while(i + 1 < str.length() && str.charAt(i) == str.charAt(i + 1)){
                count++;
                i++;
            }
            compressedStr.append(str.charAt(i));
            compressedStr.append(count);
            i++;
        }
        if(compressedStr.length() < str.length())
            return compressedStr.toString();
        else
            return str;
    }