-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
StringCompression.java
39 lines (35 loc) · 1.53 KB
/
StringCompression.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.ctci.arraysandstrings;
/**
* @author rampatra
* @since 24/11/2018
*/
public class StringCompression {
/**
* Compresses the string {@code s} such that a string {@code aabccccaaa} becomes {@code a2b1c4a3}.
* Also, if the compressed string is not shorter than the original, returns the original string.
*
* @param str input string containing only a-z characters, both cases
* @return which ever is the shorter string
*/
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;
}
public static void main(String[] args) {
System.out.println("aabccccaaa: " + compressString("aabccccaaa"));
System.out.println("aabccccAAAA: " + compressString("aabccccAAAA"));
System.out.println("abcd: " + compressString("abcd"));
System.out.println("a: " + compressString("a"));
System.out.println("aabcccccccccccccccccccccccccaaa: " + compressString("aabcccccccccccccccccccccccccaaa"));
}
}