-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompressString
More file actions
35 lines (26 loc) · 1.01 KB
/
compressString
File metadata and controls
35 lines (26 loc) · 1.01 KB
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
#error was at the bottom of the code. I was increasing
#i by 1 when it was being increaded by assigning j to i
#difficult exercise to put together...
class Solution:
def compressString(self, chars):
write_index = 0
i = 0
while i < len(chars):
j = i+1
while j < len(chars) and chars[j] == chars[i]:
j += 1
chars[write_index] = chars[i]
write_index += 1
if j - i > 1:
count = str(j-i)
print(len(count))
#slice below using :
chars[write_index:write_index+len(count)] = count
print(chars[write_index:write_index+len(count)])
write_index += len(count)
i = j
#i += 1
del chars[write_index:]
return len(chars)
myVar = Solution()
myVar.compressString(["a", "a", "b", "b", "c", "c", "c"])