jayati

Recursively remove all adjacent duplicates

Sep 22nd, 2023
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. //{ Driver Code Starts
  2. // Initial Template for C++
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. // } Driver Code Ends
  8. //User function template for C++
  9.  
  10. class Solution{
  11. public:
  12.    string rem(string s)
  13.    {
  14.        string ans="";
  15.        int n=s.length();
  16.        int i=0;
  17.        while(i<n)
  18.        {
  19.            if(i<n-1 && s[i]==s[i+1])
  20.            {
  21.                while(i<n-1 && s[i]==s[i+1])
  22.                {
  23.                    i++;
  24.                }
  25.                
  26.            }
  27.            else
  28.            {
  29.                ans+=s[i];
  30.            }
  31.            i++;
  32.        }
  33.        return ans;
  34.    }
  35.    string rremove(string s){
  36.          string s1;
  37.          while(s.size() != s1.size())
  38.          {
  39.              s1=s;
  40.              s= rem(s);
  41.          }
  42.          return s;
  43.     }
  44. };
  45.  
  46. //{ Driver Code Starts.
  47.  
  48. int main() {
  49.     int t;
  50.     string tc;
  51.     getline(cin, tc);
  52.     t = stoi(tc);
  53.     while (t--) {
  54.         string s;
  55.         getline(cin, s);
  56.         Solution ob;
  57.         cout << ob.rremove(s) << "\n";
  58.     }
  59.     return 0;
  60. }
  61. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment