| 1 | public class FindSubsets { |
| 2 | //using StringBuilder |
| 3 | public static void findSubsets(String str, int i, StringBuilder ans) { |
| 4 | //base case |
| 5 | if(i == str.length()) { |
| 6 | if(ans.length() == 0) { |
| 7 | System.out.println("null"); |
| 8 | return; |
| 9 | } |
| 10 | System.out.println(ans); |
| 11 | return; |
| 12 | } |
| 13 | |
| 14 | //recursion - make choice |
| 15 | findSubsets(str, i+1, ans.append(str.charAt(i))); |
| 16 | ans.deleteCharAt(ans.length()-1); |
| 17 | findSubsets(str, i+1, ans); |
| 18 | } |
| 19 | |
| 20 | //using string |
| 21 | public static void findSubsets(String str, int i, String ans) { |
| 22 | if(i == str.length()) { |
| 23 | if(ans.length() == 0) { |
| 24 | System.out.println("null"); |
| 25 | } else { |
| 26 | System.out.println(ans); |
| 27 | } |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | findSubsets(str, i+1, ans); |
| 32 | findSubsets(str, i+1, ans+str.charAt(i)); |
| 33 | } |
| 34 | public static void main(String args[]) { |
| 35 | String str = "abc"; |
| 36 | findSubsets(str, 0, new StringBuilder("")); |
| 37 | System.out.println("--------------------"); |
| 38 | findSubsets(str, 0, ""); |
| 39 | } |
| 40 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…