Subsequence generation

a subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements

then why the string “abc” has [ " ", "b “, “c”] not [” ",“a”,“b”,“c”]

why is A coming after in the ArrayList why not before ?

@ishaanveerd,

Let’s say the string is abcd.
Now the result of this will be a + subsequences(bcd).
Answer of bcd will be b + subsequences(cd).
Answer of cd will be c + subsequences(d).
Answer of subsequences(d) will be d + subsequences(""), this is where we hit our base case and return arraylist with “” (empty string).

Now if we see our recursion stack the calls are from bottom to up (subseq(d), subseq(cd), subseq(bcd)…)

So now, the for loop will be executed 1 time for empty string arraylist [""].

2 times for arraylist ["",d]. 4 times for arraylist ["",d,c,cd]. 8 times for ["",d,c,cd,b,bd,bc,bcd].

And then finally 16 times for ‘a’.

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.