-
Notifications
You must be signed in to change notification settings - Fork 2
/
compressString.py
48 lines (45 loc) · 1.12 KB
/
compressString.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
'''Write a program to do basic string compression. For a character which is consecutively repeated more than once, replace consecutive duplicate occurrences with the count of repetitions.
For e.g. if a String has 'x' repeated 5 times, replace this "xxxxx" with "x5".
Note : Consecutive count of every character in input string is less than equal to 9.
Input Format :
Input string S
Output Format :
Compressed string
Sample Input:
aaabbccdsa
Sample Output:
a3b2c2dsa
'''
def runLengthEncoding(st):
s = ""
n = len(st)
c=0
i=0
count = 0
while i < n:
if(st[c] == st[i]):
count = count+1
else:
s = s + st[c]
if count > 1:
s = s + str(count)
c=i
i = i-1
count=0
i = i+1
i = n-1
count = 1
while 1:
if(st[i] == st[i-1]):
count = count+1
else:
s = s+ st[n-1]
if count >1:
s = s + str(count)
break
i = i-1
return s
# Driver function
if __name__ == "__main__":
st=input()
print(runLengthEncoding(st))