Remove consecutive duplicate characters in a string C++
Remove Consecutive Duplicates
Send Feedback
For a given string(str), remove all the consecutive duplicate characters.
Example:
Input String: "aaaa"
Expected Output: "a"
Input String: "aabbbcc"
Expected Output: "abc"
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.
The only line of output prints the updated string.
Note:
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
C++ Code:
// input - given string
// You need to update in the input string itself. No need to return or print anything
void removeConsecutiveDuplicates(char input[]) {
// Write your code here
int len=0;
while(input[len]!=0)
len++;
if (len<2)
return;
int j = 0;
for (int i=1; i<len;i++)
{
if (input[j]!=input[i])
{
j++;
input[j]=input[i];
}
}
j++;
input[j]='\0';
}
Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.
0 comments:
Post a Comment