/
StringCompression.java
51 lines (43 loc) · 1.2 KB
/
StringCompression.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
48
49
50
51
// https://leetcode.com/problems/string-compression/
class Solution {
public int compress(char[] chars) {
int w = 0, r = 0, count = 0;
char last = 0;
for (; r < chars.length; r++) {
if (last == 0) {
last = chars[r];
count = 1;
} else {
if (chars[r] == last) {
count++;
} else {
chars[w++] = last;
if (count > 1) {
w += writeSize(chars, count, w);
}
count = 1;
last = chars[r];
}
}
}
if (count > 1) {
chars[w++] = last;
w += writeSize(chars, count, w);
} else {
chars[w++] = last;
}
return w;
}
int writeSize(char[] arr, int size, int start) {
Stack<Integer> stack = new Stack<>();
while (size > 0) {
stack.push(size % 10);
size /= 10;
}
int res = stack.size();
while (!stack.isEmpty()) {
arr[start++] = (char) (stack.pop() + '0');
}
return res;
}
}