diff --git a/palindrome_partitioning.java b/palindrome_partitioning.java new file mode 100644 index 00000000..f140447b --- /dev/null +++ b/palindrome_partitioning.java @@ -0,0 +1,53 @@ +// Time Complexity : O(n * 2^n) because we explore all possible partitions and palindrome checking takes O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach in three sentences only +// We use backtracking to try every possible substring starting from the current pivot index and only continue recursion if the substring is a palindrome. +// When the pivot reaches the end of the string, it means a valid palindrome partition is formed, so we add a deep copy of the current path into the result. +// Backtracking removes the last chosen substring after recursion so that other possible palindrome partition combinations can be explored correctly. + +class Solution { + public List> partition(String s) { + List> result = new ArrayList<>(); + helper(s, 0, new ArrayList<>(), result); + return result; + } + + private void helper(String s, int pivot, List path, List> result) { + // base + if (pivot == s.length()) { + result.add(new ArrayList<>(path)); + return; + } + + for (int i = pivot; i < s.length(); i++) { + String curr = s.substring(pivot, i+1); + + if (isPalindrome(curr)) { + // action + path.add(curr); + + // recurse + helper(s, i+1, path, result); + + // backtrack + path.remove(path.size()-1); + + } + } + } + + boolean isPalindrome(String s) { + int i = 0; + int j = s.length()-1; + while (i<=j) { + if (s.charAt(i++) != s.charAt(j--)) { + return false; + } + } + return true; + } +} \ No newline at end of file diff --git a/subsets.java b/subsets.java new file mode 100644 index 00000000..f5fbc14f --- /dev/null +++ b/subsets.java @@ -0,0 +1,32 @@ +// Time Complexity : O(n * 2^n) because there are 2^n possible subsets and copying each subset can take O(n) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : No + + +// Your code here along with comments explaining your approach in three sentences only +// We use backtracking to generate all possible subsets by starting from a pivot index and exploring every available choice one by one. +// At every recursive call, we add a deep copy of the current path into the result because every intermediate path itself represents a valid subset. +// Backtracking removes the last added element after recursion so that other subset combinations can be explored correctly. + +class Solution { + public List> subsets(int[] nums) { + List> result = new ArrayList<>(); + helper(nums, 0, new ArrayList<>(), result); + return result; + } + + private void helper(int[] nums, int pivot, List path, List> result) { + result.add(new ArrayList<>(path)); + for(int i = pivot; i < nums.length; i++) { + //action + path.add(nums[i]); + + // recurse + helper(nums, i+1, path, result); + + // backtrack + path.remove(path.size()-1); + } + } +} \ No newline at end of file