🎉 One-stop destination for all your technical interview Preparation 🎉
Given a string print all the permutations of string with/without spaces.
Main function.
Recursive function(here getPermutations).
#include <bits/stdc++.h>
using namespace std;
void getPermutations(string s, string op)
{
if (s.size() == 0)
{
cout << op << endl;
return;
}
string op1 = op;
string op2 = op;
op1.push_back(' ');
op1.push_back(s[0]);
op2.push_back(s[0]);
s.erase(s.begin() + 0);
getPermutations(s, op1);
getPermutations(s, op2);
return;
}
int main()
{
string s;
cin >> s;
string op = "";
op.push_back(s[0]);
s.erase(s.begin() + 0);
getPermutations(s, op);
return 0;
}