diff --git a/problem1.java b/problem1.java new file mode 100644 index 00000000..b04c8ae0 --- /dev/null +++ b/problem1.java @@ -0,0 +1,50 @@ + /** + Time Complexity = O(2^N) + Explanation: + Each element has two choices: include or exclude. + This generates all possible subsets → total 2^N subsets. + + Space Complexity = O(N) + Explanation: + Recursion depth is N and path list stores at most N elements. + + Did this code successfully run on LeetCode : Yes + + Any problem you faced while coding this : + Initially created a new list for every recursive call, + which increased space usage. + Optimized it using backtracking (add → recurse → remove), + which reuses the same list and improves efficiency. + Also ensured to copy the path when adding to result to avoid mutation issues. + */ + + class Solution { + + + + List> result; + + public List> subsets(int[] nums) { + this.result = new ArrayList<>(); + helper(nums, 0, new ArrayList<>()); + return result; + } + + private void helper(int[] nums, int i, List path) { + + if (i == nums.length) { + result.add(new ArrayList<>(path)); // important copy + return; + } + + // Not choose + helper(nums, i + 1, path); + + // Choose + path.add(nums[i]); + helper(nums, i + 1, path); + + // Backtrack + path.remove(path.size() - 1); + } + } \ No newline at end of file diff --git a/problem2.java b/problem2.java new file mode 100644 index 00000000..866dccd8 --- /dev/null +++ b/problem2.java @@ -0,0 +1,70 @@ + + /** + Time Complexity : O(N * 2^N) + Explanation: + - There are 2^N possible ways to partition the string. + - For each partition, we check substrings for palindrome (O(N)). + So overall complexity is O(N * 2^N). + + Space Complexity : O(N) + Explanation: + Recursion depth can go up to N and path stores at most N substrings. + + Did this code successfully run on LeetCode : Yes + + Any problem you faced while coding this : + Initially had difficulty deciding how to split the string dynamically. + Fixed it by using backtracking: + - Try every possible substring starting from current index + - Check if it is a palindrome + - If yes, include it in path and recurse + Also needed to backtrack properly (remove last element) + and copy the path when adding to result. + */ + + 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 case + if (pivot == s.length()) { + result.add(new ArrayList<>(path)); + return; + } + + // Try all possible substrings + for (int i = pivot; i < s.length(); i++) { + + String curr = s.substring(pivot, i + 1); + + if (isPalindrome(curr)) { + path.add(curr); + helper(s, i + 1, path, result); + path.remove(path.size() - 1); // backtrack + } + } + } + + // Check palindrome + private boolean isPalindrome(String s) { + + int i = 0; + int j = s.length() - 1; + + while (i < j) { + if (s.charAt(i) != s.charAt(j)) return false; + i++; + j--; + } + + return true; + } + } \ No newline at end of file