GetSubsequenceswithASCII in recursion get

can you help me with the code and logic behind this

@Naman_Gupta,
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’.

If you have written the code for first version, subsequences without ASCII, you just need to modify it little bit.

Only change will be to add one extra recursion call with ASCII code appending in result so far.

	for (int i = 0; i < n; i++) {
		int c = (int) str.charAt(0);
		res.add(c + res.get(i));
	}

Something like this where res is our result arraylist to be returned.

package RecursionGet; import java.util.ArrayList; public class GetSubsequnceWAscii { public static void main(String[] args) { System.out.println(GetSSWA(“ab”)); } public static ArrayList GetSSWA(String str) { if(str.length() == 0) { ArrayList BaseResult = new ArrayList<>(); BaseResult.add(" "); return BaseResult; } char cc = str.charAt(0); String ros = str.substring(1); ArrayList Myresult = new ArrayList<>(); ArrayList RecResult = GetSSWA(ros); for(int i=0; i<RecResult.size(); i++) { int c = (int) str.charAt(0); Myresult.add(RecResult.get(i)); Myresult.add(cc + RecResult.get(i)); Myresult.add(c + Myresult.get(i)); } return Myresult; } } after applies its not giving the accurate result for AB please help me more bcz recursion is now becoming typical for me

@Naman_Gupta,
Can you send your code through https://ide.codingblocks.com/ ? It is not in a readable format here.

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.