-
Notifications
You must be signed in to change notification settings - Fork 7
/
_1_6.java
30 lines (28 loc) · 1.1 KB
/
_1_6.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
package com.fisher.coder.chapter1;
/**
* Created by stevesun on 4/16/17.
*/
public class _1_6 {
/**Implement a method to perform basic string compression using the counts of repeated characters.
* For example, the string aabcccccaaa would become a2b1c5a3.
* If the "compressed" string would not become smaller than the original string, your method should return the original string.*/
public static String stringCompression(String original) {
StringBuilder stringBuilder = new StringBuilder();
char last = original.charAt(0);
int count = 1;
for (int i = 1; i < original.length(); i++) {
if (last == original.charAt(i)) {
count++;
} else {
stringBuilder.append(last);
stringBuilder.append(count);
last = original.charAt(i);
count = 1;
}
}
stringBuilder.append(last);
stringBuilder.append(count);
if (stringBuilder.toString().length() >= original.length()) return original;
return stringBuilder.toString();
}
}