import java.util.*;
public class Recursion2 {
public static void uniqueSubsequences(String str, int idx, String newString, HashSet<String> set) {
if (idx == str.length()) {
if (set.contains(newString)) {
return;
} else {
System.out.println(newString);
set.add(newString);
return;
}
}
char currentChar = str.charAt(idx);
// To be ...
uniqueSubsequences(str, idx + 1, newString + currentChar, set);
// Not to be...
uniqueSubsequences(str, idx + 1, newString, set);
}
public static void main(String[] args) {
String str = "aaa";
HashSet<String> set = new HashSet<>();
uniqueSubsequences(str, 0, "", set);
}
}
OUTPUT :
aaa
aa
a

0 Comments