Junior — Senior
String compression in one pass
livecode
Task condition
It is necessary to write a function compress that transforms the input string by replacing consecutive identical characters with the character and the number of its repetitions if repetitions are more than one. Single characters are left unchanged. Case sensitivity is mandatory.
public class StringTask {
public static void main(String[] args) {
runTest("aabccccaaa", "a2bc5a3");
runTest("abcdef", "abcdef");
runTest("AAaaBB", "A2a2B2");
}
public static void runTest(String src, String exp) {
String result = compress(src);
if (!result.equals(exp)) {
System.out.printf("Test failed for input: %s%n, expected: %s, actual: %s%n", src, exp, result);
} else {
System.out.printf("Test passed for input: %s%n", src);
}
}
public static String compress(String src) {
//
}
}