diff --git a/PalindromePartitioning.java b/PalindromePartitioning.java new file mode 100644 index 00000000..27ee243c --- /dev/null +++ b/PalindromePartitioning.java @@ -0,0 +1,42 @@ +// Time Complexity :O(n* 2^n) +// Space Complexity :O(n^2) +// Did this code successfully run on Leetcode :yes +import java.util.ArrayList; +import java.util.List; + +class Solution { + public List> partition(String s) { + List> answer = new ArrayList<>(); + List path = new ArrayList<>(); + helper(s, path, answer); + return answer; + } + private void helper(String s, List path, List> answer){ + if(s.length() == 0){ + answer.add(new ArrayList<>(path)); + return; + } + for(int i=0; i> subsets(int[] nums) { + List> answer = new ArrayList<>(); + List path = new ArrayList<>(); + helper(nums,0,path,answer); + return answer; + } + private void helper(int nums[],int index,List path, List> answer){ + //base + if(index == nums.length){ + answer.add(new ArrayList<>(path)); + return; + } + //logic + //no choose + helper(nums,index+1,path,answer); + //choose + //action + path.add(nums[index]); + //recurse + helper(nums,index+1,path,answer); + //backtrack + path.remove(path.size()-1); + } +} + + +// Time Complexity :O(n* 2^n) +// Space Complexity :O(n) +// Did this code successfully run on Leetcode :yes +class Solution { + public List> subsets(int[] nums) { + List> answer = new ArrayList<>(); + List path = new ArrayList<>(); + helper(nums,0,path,answer); + return answer; + } + private void helper(int nums[],int pivot,List path, List> answer){ + answer.add(new ArrayList<>(path)); + for (int i = pivot; i < nums.length; i++) { + // action + path.add(nums[i]); + // recurse + helper(nums, i + 1, path,answer); + // backtrack + path.remove(path.size() - 1); + } + } +}