-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex443.py
31 lines (27 loc) · 874 Bytes
/
ex443.py
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
from typing import List
class Solution:
def compress(self, chars: List[str]) -> int:
n = len(chars)
i = ans = 0
while i < n:
j = i
last_c = chars[i]
while j < n and chars[j] == last_c:
j += 1
if j - i == 1:
chars[ans] = last_c
else:
chars[ans] = last_c
char = list(str(j-i))
chars[ans+1:ans+1+len(char)] = char
ans += len(char)
ans += 1
i = j
return ans
if __name__ == '__main__':
solution = Solution()
chars = ["a", "a", "b", "b", "c", "c", "c"]
chars = ["a"]
chars = ["a", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b"]
chars = ["a", "a", "b", "b", "c", "c", "c", "&", "a", "z", "0"]
print(solution.compress(chars))