Advertisement
thewitchking

Untitled

May 29th, 2025
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. import java.util.*;
  2. class Main {
  3.    
  4.     public List<List<Character>> subsets(String s) {
  5.         List<List<Character>> list = new ArrayList<>();
  6.         char [] c = s.toCharArray();
  7.         backtrack(list, new ArrayList<>(), c, 0);
  8.         return list;
  9.     }
  10.  
  11.     private void backtrack(List<List<Character>> list , List<Character> tempList, char [] cs, int start){
  12.         if(!tempList.isEmpty())
  13.             list.add(new ArrayList<>(tempList));
  14.         for(int i = start; i < cs.length; i++){
  15.             tempList.add(cs[i]);
  16.             backtrack(list, tempList, cs, i + 1);
  17.             tempList.remove(tempList.size() - 1);
  18.         }
  19.     }
  20.     public static void main(String[] args) {
  21.         System.out.println(new Main().subsets("xyz"));
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement