From 9e2f79692149e8869c0972a0a638e6d0cb0abddf Mon Sep 17 00:00:00 2001 From: Hriday-A Date: Sun, 17 May 2026 19:47:14 -0500 Subject: [PATCH] Backtracking-2 Assignment Completed --- palindrome_partionining.java | 32 +++++++++++++++++++++++++ subsets.java | 46 ++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 palindrome_partionining.java create mode 100644 subsets.java diff --git a/palindrome_partionining.java b/palindrome_partionining.java new file mode 100644 index 00000000..736687a9 --- /dev/null +++ b/palindrome_partionining.java @@ -0,0 +1,32 @@ + +class Solution { + List> result; + public List> partition(String s) { + this.result= new ArrayList<>(); + helper(s,0,new ArrayList<>()); + return result; + } + private void helper(String s,int pivot,List path){ + if(pivot == s.length()){ + result.add(new ArrayList<>(path)); + return; + } + + for(int i=pivot; i> result; +// public List> subsets(int[] nums) { +// this.result = new ArrayList<>(); +// List path = new ArrayList<>(); +// helper(nums, 0, path); +// return result; +// } +// private void helper(int[] nums, int i,List path){ +// //base +// if(i==nums.length) +// { +// result.add(new ArrayList<>(path)); +// return; +// } +// //logic +// //0 +// helper(nums,i+1,path); +// path.add(nums[i]); +// helper(nums,i+1,path); +// path.remove(path.size()-1); +// } +// } + +//Time Complexity: O(n × 2ⁿ) +//Space Complexity: O(n) +class Solution { + List> result; + public List> subsets(int[] nums) { + this.result = new ArrayList<>(); + List path = new ArrayList<>(); + helper(nums,path,0); + return result; + } + private void helper(int[] nums,List path,int pivot){ + result.add(new ArrayList(path)); + for(int i=pivot;i