From 6f004d43717d37b944722c82197d67eeebc3dd46 Mon Sep 17 00:00:00 2001 From: yashhh-23 Date: Fri, 31 Jul 2026 20:57:14 +0530 Subject: [PATCH] completed backtracking 3 --- Subsets.java | 20 ++++++++++++++++++++ palindromepartition.java | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 Subsets.java create mode 100644 palindromepartition.java diff --git a/Subsets.java b/Subsets.java new file mode 100644 index 00000000..418c52b1 --- /dev/null +++ b/Subsets.java @@ -0,0 +1,20 @@ +// Time Complexity : O(2^n) where n is the number of elements in the input array. This is because for each element, we have two choices: include it in a subset or exclude it. +// Space Complexity : O(n) where n is the number of elements in the input array. This is the space required to store all possible subsets. +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : None +// Your code here along with comments explaining your approach : subsets are generated iteratively by starting with an empty subset and for each number in the input array, we create new subsets by adding that number to all existing subsets. This way, we build up all possible combinations of subsets. +class Solution { + public List> subsets(int[] nums) { + List> result = new ArrayList<>(); + result.add(new ArrayList<>()); + for (int i = 0; i < nums.length; i++) { + int size = result.size(); + for (int j = 0; j < size; j++) { + List temp = new ArrayList<>(result.get(j)); + temp.add(nums[i]); + result.add(temp); + } + } + return result; + } +} \ No newline at end of file diff --git a/palindromepartition.java b/palindromepartition.java new file mode 100644 index 00000000..23cb04c7 --- /dev/null +++ b/palindromepartition.java @@ -0,0 +1,40 @@ +// Time Complexity : O(2^n) where n is the length of the input string. This is because for each character in the string, we have two choices: either to include it in the current partition or not, leading to a binary tree of possibilities. +// Space Complexity : O(n) where n is the length of the input string. This is the space required to store the recursion stack and the resulting partitions. +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : Understanding the backtracking approach for generating all possible partitions. +// Your code here along with comments explaining your approach : used backtracking to generate all possible partitions of the input string. For each substring, we check if it is a palindrome. If it is, we add it to the current path and recursively call the helper function with the remaining substring. When we reach the end of the string, we add the current path to the result list. +class Solution { + List> result; + public List> partition(String s) { + this.result = new ArrayList<>(); + helper(s, new ArrayList<>()); + return result; + } + + private void helper(String s, List path){ + if(s.length() == 0){ + result.add(new ArrayList<>(path)); + return; + } + for(int i=0; i