-
Notifications
You must be signed in to change notification settings - Fork 0
/
443. String Compression
35 lines (34 loc) · 1.09 KB
/
443. String Compression
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
class Solution:
def compress(self, chars):
"""
:type chars: List[str]
:rtype: int
"""
position = 1
word_number = 1
while position < len(chars):
# print(chars)
if chars[position] == chars[position - 1]:
word_number += 1
chars.pop(position)
else:
if word_number == 1:
position += 1
else:
for j in range(len(str(word_number))):
chars.insert(position, str(word_number)[j])
position += 1
word_number = 1
position += 1
# print(chars)
# print('position is ', position)
if word_number == 1:
pass
else:
# print(str(word_number))
for j in range(len(str(word_number))):
chars.insert(position, str(word_number)[len(str(word_number))- 1 - j])
print(chars)
return len(chars)
intance = Solution()
print(intance.compress(["a"]))