-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrle.py
74 lines (55 loc) · 1.55 KB
/
rle.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
https://leetcode.com/problems/string-compression
Given string, possible empty:
AAAABBBCCXYZDDDDEEEFFFAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBB
we need to implement RLE (run length encoding) which produce following result:
A4B3C2XYZD4E3F3A6B28
Notes:
If symbol appears only once it doesn't changed
If symbol appears more than once, it will be added number of repetitions
"""
def rle(chars):
ll = len(chars)
if ll < 2:
return ll
r_idx, w_idx = 1, 0
while r_idx < ll:
cnt = 1
while r_idx < ll and chars[r_idx - 1] == chars[r_idx]:
r_idx += 1
cnt += 1
chars[w_idx] = chars[r_idx - 1]
w_idx += 1
if cnt > 1:
cur_cnt = str(cnt)
for s in cur_cnt:
chars[w_idx] = s
w_idx += 1
if r_idx == ll - 1:
chars[w_idx] = chars[r_idx]
w_idx += 1
r_idx += 1
return w_idx
def rle2(a):
ll = len(a)
if ll < 2:
return ll
w_idx, r_idx = 1, 1
while r_idx < ll:
if a[r_idx] == a[r_idx - 1]:
cnt = 1
while r_idx < ll and a[r_idx] == a[r_idx - 1]:
r_idx += 1
cnt += 1
for b in str(cnt):
a[w_idx] = b
w_idx += 1
if r_idx == w_idx and r_idx < ll:
if a[w_idx - 1] == a[r_idx]:
r_idx += 1
w_idx += 1
else:
a[w_idx] = a[r_idx]
w_idx += 1
r_idx += 1
return w_idx