forked from Upendradwivedi/python-programs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremoveDuplicateChar.py
More file actions
40 lines (29 loc) · 856 Bytes
/
removeDuplicateChar.py
File metadata and controls
40 lines (29 loc) · 856 Bytes
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
# Python3 program to remove consecutive duplicates from a given string.
# A iterative function that removes consecutive duplicates from string S
def removeDuplicates(S):
n = len(S)
# We don't need to do anything for empty or single character string.
if (n < 2) :
return
# j is used to store index is result string (or index of current distinct character)
j = 0
# Traversing string
for i in range(n):
# If current character S[i] is different from S[j]
if (S[j] != S[i]):
j += 1
S[j] = S[i]
# Putting string termination character.
j += 1
S = S[:j]
return S
# Driver Code
if __name__ == '__main__':
S1 = "Solluttions"
S1 = list(S1.rstrip())
S1 = removeDuplicates(S1)
print(*S1, sep = "")
S2 = "aabccadseeefff"
S2 = list(S2.rstrip())
S2 = removeDuplicates(S2)
print(*S2, sep = "")