diff --git a/Problem1.java b/Problem1.java new file mode 100644 index 00000000..2261837b --- /dev/null +++ b/Problem1.java @@ -0,0 +1,34 @@ +//TC - O(2^n) +//SC - O(N) + +/* +The subsets method initializes the result list and starts backtracking from index 0 with an empty subset. +The backtrack method adds the current subset to the result and recursively explores all choices by including each element from the current index onward. +After each recursive call, the last added element is removed to backtrack and generate the remaining subsets. +*/ + + +import java.util.ArrayList; +import java.util.List; + +public class Problem1 { + public List> subsets(int[] nums) { + List> res = new ArrayList<>(); + backtrack(nums, res, new ArrayList(), 0); + return res; + } + + private void backtrack(int[] nums, List> res, List temp, int pivot) { + res.add(new ArrayList<>(temp)); + + //logic + for(int i = pivot;i> partition(String s) { + List> res = new ArrayList<>(); + helper(s, res, new ArrayList<>(), 0); + return res; + } + + private void helper(String s, List> res, List temp, int pivot) { + //base + if(pivot == s.length()) { + res.add(new ArrayList<>(temp)); + return; + } + //logic + for(int i = pivot;i