https://leetcode.com/problems/string-compression/
- String
Use write pointer to compress in-place. Count consecutive characters and update chars array.
O(n)
O(1)
class Solution {
public int compress(char[] chars) {
int write = 0;
for (int i = 0; i < chars.length; ) {
char c = chars[i];
int count = 0;
while (i < chars.length && chars[i] == c) {
count++;
i++;
}
chars[write++] = c;
if (count > 1) {
for (char d : String.valueOf(count).toCharArray()) {
chars[write++] = d;
}
}
}
return write;
}
}