#include <bits/stdc++.h> using namespace std; void solve(string str, string output, int index, vector<string>& ans) { //base case if(index >= str.length()) { if(output.length()>0) ans.push_back(output); return; } //exclude solve(str, output, index+1, ans); //include char element = str[index]; output.push_back(element); solve(str, output, index+1, ans); } vector<string> subsequences(string str) { vector<string> ans; string output = ""; int index = 0; solve(str, output, index, ans); return ans; } int main() { string s="abcd"; vector<string> v=subsequences(s); for(int i=0;i<v.size();i++) { cout<<v[i]<<" "; } return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter