I'm getting the wrong output[97, 98, a98, 97, , a, 97, b, ab] like this..... Please tell me what is my mistake in this code

import java.util.ArrayList;

public class Subsequencesascii {

public static void main(String[] args) {
	
	System.out.println(getascii("ab"));

}

public static ArrayList<String> getascii(String str){
	
	if(str.length()==0) {
		ArrayList<String> baseres = new ArrayList<String>();
		baseres.add("");
		return baseres;
	}
	
	char cc = str.charAt(0);
	int voc = cc;
	String ros = str.substring(1);
	
	ArrayList<String> myres = new ArrayList<String>();
	
	ArrayList<String> recres = getascii(ros);
	
	for(int i=0 ; i<recres.size() ; i++) {
		myres.add(voc + "");
		myres.add(recres.get(i));
		myres.add(  cc + recres.get(i));
		
	}
	
	return myres;
	
	
}

}

This question is the assignment question to get subsequences with ascii

@LakshmiPrasanna0303,
https://ide.codingblocks.com/s/233623 corrected code. I have added comments on the lines I have edited.

1 Like