-
Notifications
You must be signed in to change notification settings - Fork 17
/
RunLengthEncoding.py
34 lines (25 loc) · 996 Bytes
/
RunLengthEncoding.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
'''
Run-Length Encoding:
Encode string as follows:
Input: XXXXTTTPPPPPPPPPP
Output: 4X3T9P1P
Time: O(N), where N is the number of elements in the input array
Space: O(N), where N is the number of elements in the input array
Space (Worst): O(2N) -> O(N), when all characts in array are distinct
Last Practiced: 2022-03-13 08:42:45
'''
def runLengthEncoding(string):
currentRunLength = 1
encodedString = []
for i in range(1,len(string)):
currentChar = string[i]
previousChar = string[i-1]
if currentChar != previousChar or currentRunLength == 9:
encodedString.append(str(currentRunLength))
encodedString.append(previousChar)
currentRunLength = 0
currentRunLength += 1
encodedString.append(str(currentRunLength))
encodedString.append(string[len(string)-1])
return "".join(encodedString)
print(runLengthEncoding("abcdefghijklmnopqrstuvwxyz"))