-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
41 lines (33 loc) · 1002 Bytes
/
solution.py
File metadata and controls
41 lines (33 loc) · 1002 Bytes
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
class Solution(object):
def compress(self, chars):
"""
:type chars: List[str]
:rtype: int
"""
char = None
count = 0
length = 0
for c in chars:
if char == None:
count = 1
char = c
elif char == c:
count += 1
elif char != c:
chars[length] = char
length += 1
if count > 1:
count_str = str(count)
for c in count_str:
chars[length] = c
length += 1
count = 1
char = c
chars[length] = char
length += 1
if count > 1:
count_str = str(count)
for c in count_str:
chars[length] = c
length += 1
return length