Print all the possible non empty sub sequences
Anonymous User
10246

I just now solved 1079. Letter Tile Possibilities
The questions decribes to count the no of non-empty sequences of letter

input: tiles = "AAB"
Output: 8
Explanation: The possible sequences are "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA".

I also want print all the sequences of letters
I tried using the below approach but failed to get the correct output.
My code:

#include <bits/stdc++.h>
#include <iterator>
#include <set>
using namespace std;
void solve(vector<string>&st,string sr,int i){
    //this function is to get all the permutations of the string
	if(i>=sr.size()){
		st.push_back(sr);
		return;
	}
	for(int k=i;k<=sr.size();k++){
		swap(sr[i],sr[k]);
		solve(st,sr,i+1);
		swap(sr[i],sr[k]);
	}		
}
int main(){
	string s;
	cin>>s;
	vector<string>st;

    for(int i=0;i<s.size();i++){
        for(int j=i;j<s.size();j++){
            string m=s.substr(i,j);
            solve(st,m,0);
            
        }
    }
    set<string>ste;
    for(int i=0;i<st.size();i++){
        ste.insert(st[i]);
    }
	cout<<"No.of permutations is:"<<ste.size()<<endl;
	for(auto it=ste.begin();it!=ste.end();++it){
		cout<<*it<<" ";
	}
	
}

I cannot get the correct approach.Can anyone give the solution for this??

Comments (6)