Remove Consecutive Duplicates From String
You are given a string 'STR' consisting of lower and upper case characters. You need to remove the consecutive duplicates characters, and return the new string.
Example :
For a given string, "aaaAAbbcccbd"
The new string formed after removing the consecutive duplicates characters will be, "aAbcbd".
Input Format :
The first and the only line of input contains a string 'STR' with no space in between the characters.
Output Format :
Print the final string after removing all the consecutive duplicates.
Note :
You don't have to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= |S| <= 10^5
Where |S| represents the length of the string.
Time limit: 1 sec
AnswerBot
1y
The task is to remove consecutive duplicate characters from a given string and return the new string.
Iterate through the characters of the string
Compare each character with the next character
If they a...read more
CodingNinjas
author
2y
First I explained to him the brute force with pseudo code.
-> Recursively calling the same method, until the compression is possible.
-> Complexity : O(n^2)
Also explained to him an optimized solution u...read more
CodingNinjas
author
2y
Stack - Based Approach
Approach :
- This is a stack-based approach where we will iterate through the string from the right side.
- While iterating we will check if the top of the stack is not equal to the cu...read more
CodingNinjas
author
2y
Two Pointer Approach
Let our input string be
str =
where str[i] denotes the i'th character of the string and n denotes the length of the string.
STEPS
- Initialize an empty st...read more
CodingNinjas
author
2y
Space Optimsed Two Pointer Approach
Algorithm:
- Create two variables ‘i’ and ‘j’ both initialized to 0.
- Now run a loop till ‘j’ < length of the input string ‘STR’ and perform the following steps:
- Now keep...read more
Add answer anonymously...
Top Meesho SDE-2 interview questions & answers
Popular interview questions of SDE-2
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app