Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions problem1.java
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> result;

public List<List<Integer>> subsets(int[] nums) {
this.result = new ArrayList<>();
helper(nums, 0, new ArrayList<>());
return result;
}

private void helper(int[] nums, int i, List<Integer> 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);
}
}
70 changes: 70 additions & 0 deletions problem2.java
Original file line number Diff line number Diff line change
@@ -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<List<String>> partition(String s) {

List<List<String>> result = new ArrayList<>();
helper(s, 0, new ArrayList<>(), result);
return result;
}

private void helper(String s, int pivot, List<String> path, List<List<String>> 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;
}
}