For string s = "abcd" ,k=3 then answer should be:
abc
abd
acd
bcd
code by java with recursion(k=3) :
public class SubString {
static ArrayList<String> al = new ArrayList<>();
public static void main(String[] args) {
String s = "abcd";
findsubsequences(s, ""); // Calling a function
for (String subString : al) {
if (subString.length() == 3) {
System.out.println(subString);
}
}
}
public static void findsubsequences(String s, String ans) {
if (s.length() == 0) {
al.add(ans);
return;
}
findsubsequences(s.substring(1), ans + s.charAt(0));
findsubsequences(s.substring(1), ans);
}
}
I want to Find all possible substring of length k in fastest way by recursion and without foreach in arraylist