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
53 changes: 53 additions & 0 deletions palindrome_partitioning.java
Original file line number Diff line number Diff line change
@@ -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<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
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;
}
}
32 changes: 32 additions & 0 deletions subsets.java
Original file line number Diff line number Diff line change
@@ -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<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
helper(nums, 0, new ArrayList<>(), result);
return result;
}

private void helper(int[] nums, int pivot, List<Integer> path, List<List<Integer>> 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);
}
}
}