Remove consecutive duplicate characters in a string C++
Remove Consecutive Duplicates
Send Feedback
C/C++,java,python c++ keywords, identifiers, data types,variables,modifiers,storage classes,operators,for loop ,while loop,do while loop,if else statement,nested loops,break continue goto statement,functions ,call by value call by reference ,arrays ,strings,pointers,data structures ,classes and objects,inheritance,polymorphism,operator overloading,function overloading,data abstraction,data encapsulation,dynamic memory allocation for array and objects,function templates,class templates,
Remove consecutive duplicate characters in a string C++
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.
You are not required to print anything. It has already been taken care of.
0 <= N <= 10^6
Where N is the length of the input string.
Time Limit: 1 second
aabccbaa
abcba
xxyyzxx
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'; }
0 comments:
Post a Comment