1
class Solution {
2
public int compress(char[] chars) {
3
int index = 0;
4
int i = 0;
5

6
while (i < chars.length) {
7
int j = i;
8

9
while (j < chars.length && chars[j] == chars[i]) {
10
j++;
11
}
12

13
chars[index++] = chars[i];
14

15
if (j - i > 1) {
16
String count = j - i + "";
17

18
for (char c : count.toCharArray()) {
19
chars[index++] = c;
20
}
21
}
22

23
i = j;
24
}
25

26
return index;
27
}
28
}
29

30
// TC: O(n), SC: O(1)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0