描述
设计一种方法,通过给重复字符计数来进行基本的字符串压缩。
例如,字符串 aabcccccaaa 可压缩为 a2b1c5a3 。而如果压缩后的字符数不小于原始的字符数,则返回原始的字符串。
可以假设字符串仅包括 a-z 的大/小写字母。
在线评测地址:领扣题库官网
样例1
Input: str = "aabcccccaaa"
Output: "a2b1c5a3"
样例2
Input: str = "aabbcc"
Output: "aabbcc"
源代码
public class Solution {
/**
* @param str a string
* @return a compressed string
*/
public String compress(String str) {
// Write your code here
/* Check if compression would create a longer string */
int size = countCompression(str);
if (size >= str.length()) {
return str;
}
char[] array = new char[size];
int index = 0;
char last = str.charAt(0);
int count = 1;
for (int i = 1; i < str.length(); i++) {
if (str.charAt(i) == last) { // Found repeated character
count++;
} else {
/* Update the repeated character count */
index = setChar(array, last, index, count);
last = str.charAt(i);
count = 1;
}
}
/* Update string with the last set of repeated characters. */
index = setChar(array, last, index, count);
return String.valueOf(array);
}
public int setChar(char[] array, char c, int index, int count) {
array[index] = c;
index++;
char[] cnt = String.valueOf(count).toCharArray();
/* Copy characters from biggest digit to smallest */
for (char x : cnt) {
array[index] = x;
index++;
}
return index;
}
int countCompression(String str) {
if (str == null || str.isEmpty()) return 0;
char last = str.charAt(0);
int size = 0;
int count = 1;
for (int i = 1; i < str.length(); i++) {
if (str.charAt(i) == last) {
count++;
} else {
last = str.charAt(i);
size += 1 + String.valueOf(count).length();
count = 1;
}
}
size += 1 + String.valueOf(count).length();
return size;
}
}
更多题解参考:九章官网solution