🎉 One-stop destination for all your technical interview Preparation 🎉
Print all subsets of a given string using recursion.
Main function.
Recursive function(here getSubstring).
If want only unique subsets then store the values in unordered_set/set and you can print them later.
#include <bits/stdc++.h>
using namespace std;
void getSubstring(string s, string op)
{
if (s.size() == 0)
{
cout << op << ' ';
return;
}
string op1 = op;
string op2 = op;
op2.push_back(s[0]);
s.erase(s.begin() + 0);
getSubstring(s, op1);
getSubstring(s, op2);
return;
}
int main()
{
string s;
cin >> s;
string op = "";
getSubstring(s, op);
return 0;
}