If I have understood correctly, you're aiming for all subset of a String. Through your code for String = "ABC"; //Result is -- > A AB ABC AC B BC C
However, for
String = "ABB"; // Result is --> A AB ABB AB B BB B (You see AB twice as well as letter B).
So to make it more clear for unique subsets, added a set implementation.
TreeSet (extra sorted feature over hash), as below : (using same recursion)
public class Test { public static void allSet(String s, String t, Set<String> set) { // System.out.print(t + " "); will print later. if(!"".equalsIgnoreCase(t)) set.add(t); if (s.length() > 0) { for (int i = 0; i < s.length(); i++) { allSet(s.substring(i + 1, s.length()), t + s.charAt(i),set); } } } public static void main(String args[]) { String a = "ABB"; String t = ""; Set<String> set = new TreeSet<String>(); allSet(a, t, set); System.out.println(set.toString()); } // Now, result --> [A, AB, ABB, B, BB]