« Remove duplicates recursively
Problem
Given a string S, remove consecutive duplicates from it recursively.
Input Format :
String S
Output Format :
Output string
Constraints :
1 <= |S| <= 10^3
where |S| represents the length of string
Sample Input 1 :
aabccba
Sample Output 1 :
abcba
Sample Input 2 :
xxxyyyzwwzzz
Sample Output 2 :
xyzwz
Solution
1#include <iostream>2#include <iomanip>3#include <algorithm>4#include <string>5#include <cstring>6#include <vector>7#include <cmath>8#include <map>9#include <climits>10// climits for INT_MIN11#include <unordered_map>12using namespace std;1314void removeConsecutiveDuplicates(char *input) {15 if(input[0] == '\0' || input[1] == '\0'){16 return;17 }18 if(input[0] == input[1]){19 int i = 0;20 while(input[i] != '\0'){21 input[i] = input[i+1];22 i++;23 }24 removeConsecutiveDuplicates(input);25 }26 removeConsecutiveDuplicates(input + 1);27}2829int main()30{31 char str[1000000];32 cin >> str;33 removeConsecutiveDuplicates(str);34 cout << str << endl;35 return 0;36}