Remove Consecutive Duplicates

 Problem Statement

Suggest Edit

For a given string(str), remove all the consecutive duplicate characters.

Example:
Input String: "aaaa"
Expected Output: "a"

Input String: "aabbbcc"
Expected Output: "abc"
 Input Format:
The first and only line of input contains a string without any leading and trailing spaces. All the characters in the string would be in lower case.
Output Format:
The only line of output prints the updated string.

You are not required to print anything. It has already been taken care of.
Constraints:
0 <= N <= 10^6
Where N is the length of the input string.

Time Limit: 1 second
Sample Input 1:
aabccbaa
Sample Output 1:
abcba
Sample Input 2:
xxyyzxx
Sample Output 2:
xyzx

from sys import stdin

def removeConsecutiveDuplicates(string) :
    str=""
    for i in string:
        if str=='':
            str+=i
            prev=str[len(str)-1]
        else:
        	prev=str[len(str)-1]
        if i!=prev:
            str+=i
        else:
            continue
            
    return str
        
                
            

    #Your code goes here.

















    
            


#main
string = stdin.readline().strip()

ans = removeConsecutiveDuplicates(string)

print(ans)

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings