Subsequences of string

PHOTO EMBED

Sun Jan 29 2023 07:42:19 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

#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;
}
content_copyCOPY