I compressed my binary string and it's outputting 3131 for 6 1s(111111) which represents the character 'p' from this code by @tigrang I found in this link.
public static String compress_string(String inp) {
String compressed = "";
Pattern pattern = Pattern.compile("([\\w])\\1*");
Matcher matcher = pattern.matcher(inp);
while(matcher.find()) {
String group = matcher.group();
if (group.length() > 1) compressed += group.length() + "";
compressed += group.charAt(0);
}
return compressed;
}
Now I need to uncompress this string "3131" and make it output 111111. How can I do this without using a loop? And is there any way to compress it even more, for eg: outputting 61 instead of 3131?