i am returning the vector containing all subsequences,problem arising in line number 9 to 13
Problem in traversing vector
Hi @narayansatish
Problem is arising because when you are passing vector to sub function, then it is empty in first call and in there you are iterating it from starting to end but as your vector is empty so it dont go in for loop in first call and then in second call also as you have not pushed any element into the vector so in every call for loop doesnt executes. Instead you should use a simple logic of recursion in which you pass string s and another null string ans and then take ch as s[0] and then make two calls to the same function one in which you consider ch as ans+ch and another where you just send ans. This is how you will be able to generate all the substrings.
here is the code for your better understanding.
but my vector is not empty, in main i have push_back “” this one. you can see after execution of that loop strings are getting push_backed. you can see vector after execution
Okay… But every time when you are printing *it it is just printing null string. Instead of iterator use (*v).size().
Run loop of i from 0 to v->size().
int x=v->size();
for(int i=0;i<x;i++)
{
d=(*v)[i]+c;
cout<<“d is”<<d<<endl;
v->push_back(d);
}
Using this you will be able to generate all the subsequences of the string.
thank you! but why iterator is behaving like this?
one more question ,i am getting correct ouput ,but when i am
but when i am submitting it is giving verdict as wrong answer
Hello @narayansatish
You output for input string “abcd” does NOT match the sample output for “abcd”
Your output
a b ab c ac bc abc d ad bd abd cd acd bcd abcd
16
Correct output
d c cd b bd bc bcd a ad ac acd ab abd abc abcd
16
Here is the modified code which produces output as per the sample test case.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.