Problem Statement:
Reverse the queue using recursion
Example:
Input: queue = [1, 2, 3, 4]
Output: res = [4, 3, 2, 1]
Solution Explanation:
Check if the current char is equal to the next, if yes remove both of them.
Time Complexity: O(n*n)
Space Complexity: O(n)
Code Solution
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
string solution(string str)
{
int len = str.size();
if(len==1)
return str;
for(auto i=0; i<len-1; i++)
{
if(str[i]==str[i+1])
{
str.erase(i,2);
break;
}
}
if(len==str.size())
return str;
return solution(str);
}
int main()
{
string s = "hello";
cout << solution(s) << endl;
return 0;
}
Output
heo