String Compression
约 1 分钟· 本文阅读 次
本页目录
String Compression
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. You can assum the string has only uppercase and lowercase letters(a-z)
Solution
/**
* check the length of the new string in the first place and then build
* the new string with stringBuilder(if needed)
*/
public static String compressString(String str){
StringBuilder compressed = new StringBuilder(str.length());
int count = 0;
for (int i = 0; i < str.length(); i++){
count++;
if (i+1 == str.length() || str.charAt(i) != str.charAt(i+1)){
compressed.append(str.charAt(i));
compressed.append(count);
count = 0;
}
}
if (compressed.toString().length() >= str.length()){
return str;
}
return compressed.toString();
}
public static int countLength(String str){
int compressedLength = 0;
int countConsecutive = 0;
for (int i = 0; i < str.length(); i++){
countConsecutive++;
if (i + 1 == str.length() || str.charAt(i) != str.charAt(i+1)){
compressedLength += 1
+ String.valueOf(countConsecutive).length();
countConsecutive = 0;
}
}
return compressedLength;
}相关文章
- 2 Sum2016-01-05
- 3 Sum Closest2016-01-05
- 3 Sum2016-01-05
- 4 Sum2016-01-05
已恢复到上次阅读位置