-
Notifications
You must be signed in to change notification settings - Fork 2
/
CompressedString.java
47 lines (40 loc) · 1.29 KB
/
CompressedString.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
40
41
42
43
44
45
46
47
/*
String Compression: Implement a method to perform basic string compression using the counts
of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the
"compressed" string would not become smaller than the original string, your method should return
the original string. You can assume the string has only uppercase and lowercase letters (a - z).
*/
public class CompressedString{
public static void main(String[] args){
String s1 = "abcdefghijklmnop";
String s2 = "aabcccccaaa";
String s3 = "ddeeff";
String s4 = "aaaaaaaaaaaaaaaaaa";
String s5 = "addfffgggg";
String s6 = "a";
Test t1 = new Test();
System.out.println(t1.compString(s1));
System.out.println(t1.compString(s2));
System.out.println(t1.compString(s3));
System.out.println(t1.compString(s4));
System.out.println(t1.compString(s5));
System.out.println(t1.compString(s6));
}
}
class Test{
public String compString(String s){
StringBuilder sb = new StringBuilder();
int count = 1;
for(int i = 0 ; i < s.length() ; i++){
char c = s.charAt(i);
if(i+1 >= s.length() || (c != s.charAt(i+1))){
sb.append(c).append(count);
count = 1;
}
else{
count++;
}
}
return (sb.length() > s.length()) ? s : sb.toString();
}
}