Python Program To Recursively Remove All Adjacent Duplicates
Last Updated :
31 Jan, 2023
Given a string, recursively remove adjacent duplicate characters from the string. The output string should not have any adjacent duplicates. See the following examples.
Examples:
Input: azxxzy
Output: ay
First “azxxzy” is reduced to “azzy”.
The string “azzy” contains duplicates,
so it is further reduced to “ay”.
Input: geeksforgeeg
Output: gksfor
First “geeksforgeeg” is reduced to
“gksforgg”. The string “gksforgg”
contains duplicates, so it is further
reduced to “gksfor”.
Input: caaabbbaacdddd
Output: Empty String
Input: acaaabbbacdddd
Output: acac
The following approach can be followed to remove duplicates in O(N) time:
- Start from the leftmost character and remove duplicates at left corner if there are any.
- The first character must be different from its adjacent now. Recur for string of length n-1 (string without first character).
- Let the string obtained after reducing right substring of length n-1 be rem_str. There are three possible cases
- If first character of rem_str matches with the first character of original string, remove the first character from rem_str.
- If remaining string becomes empty and last removed character is same as first character of original string. Return empty string.
- Else, append the first character of the original string at the beginning of rem_str.
- Return rem_str.
Below image is a dry run of the above approach:
Below is the implementation of the above approach:
Python
def removeUtil(string, last_removed):
if len (string) = = 0 or len (string) = = 1 :
return string
if string[ 0 ] = = string[ 1 ]:
last_removed = ord (string[ 0 ])
while len (string) > 1 and
string[ 0 ] = = string[ 1 ]:
string = string[ 1 :]
string = string[ 1 :]
return removeUtil(string, last_removed)
rem_str = removeUtil(string[ 1 :],
last_removed)
if len (rem_str) ! = 0 and
rem_str[ 0 ] = = string[ 0 ]:
last_removed = ord (string[ 0 ])
return (rem_str[ 1 :])
if len (rem_str) = = 0 and
last_removed = = ord (string[ 0 ]):
return rem_str
return ([string[ 0 ]] + rem_str)
def remove(string):
last_removed = 0
return toString(removeUtil(toList(string),
last_removed))
def toList(string):
x = []
for i in string:
x.append(i)
return x
def toString(x):
return ''.join(x)
string1 = "geeksforgeeg"
print remove(string1)
string2 = "azxxxzy"
print remove(string2)
string3 = "caaabbbaac"
print remove(string3)
string4 = "gghhg"
print remove(string4)
string5 = "aaaacddddcappp"
print remove(string5)
string6 = "aaaaaaaaaa"
print remove(string6)
string7 = "qpaaaaadaaaaadprq"
print remove(string7)
string8 = "acaaabbbacdddd"
print remove(string8)
string9 = "acbbcddc"
print remove(string9)
|
Output:
gksfor
ay
g
a
qrq
acac
a
Time Complexity: The time complexity of the solution can be written as T(n) = T(n-k) + O(k) where n is length of the input string and k is the number of first characters which are same. Solution of the recurrence is O(n)
Space Complexity: O(n),The space complexity is O(n) as we are using a recursive stack and the recursive stack is of size n.
Thanks to Prachi Bodke for suggesting this problem and initial solution.
Please refer complete article on Recursively remove all adjacent duplicates for more details!
Share your thoughts in the comments
Please Login to comment...