-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path443-String Compression.py
More file actions
36 lines (31 loc) · 1.2 KB
/
443-String Compression.py
File metadata and controls
36 lines (31 loc) · 1.2 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
36
class Solution:
#T=O(n), S=O(1)
def compress(self, chars: List[str]) -> int:
#init slow and fast pointers
s = f = 0
n = len(chars)
while f < n:
#on each iteration the fast pointer would be at a distinct digit
#slow pointer would be at location where digit value has to be overwritten
chars[s] = chars[f]
#init count to 1
count = 1
#shift fast pointer when we see identical values
while f+1 < n and chars[f] == chars[f+1]:
#increment count of current char and shift fast pointer
count += 1
f += 1
#if count of current char is greater than 1
if count > 1:
#str() op would be O(1) because len(char) < 2000
#12 should be treated as "1", "2"
for c in str(count):
#in-place update the value
chars[s+1]= c
#shift slow pointer
s += 1
#shift both slow and fast pointers
f += 1
s += 1
#return slow pointer
return s