Given a string, create a new string with all the consecutive duplicates removed.
Hint: You may make a new string to store the result. You can check whether the current character and the next character are the same, then add that character to the new string.
Note: Only consecutive letters are removed not all duplicate occurences of a letter. You may try doing this alternative i.e. removing all duplicate letters from a given string, for your own practice.
Sample Input 1:
AAABBBBCDDBBECE
Sample Output 1:
ABCDBECE
Sample Input 2:
Jupyter Notebook is better. Case sensitivity check AAaaaAaaAAAa.
Sample Output 2:
Jupyter Notebok is beter. Case sensitivity check AaAaAa.
print('Enter string: ')
str = input()
rezstr = ""
i = 0
while i < len(str):
rezstr = rezstr + str[i]
i += 1
if i == len(str):
break
while (str[i-1] == str[i]) & (i < len(str)):
i += 1
if i == len(str):
break
print('Output:')
print(rezstr)
Comments
Leave a comment