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
45 changes: 45 additions & 0 deletions PalindromePartitioning.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Time Complexity : O(n * (2 ^ n))
// Space Complexity : O(n ^ 2)
// 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
/*
We use a for loop based recursion approach where we follow the steps of taking action, recurse and backtrack.
However, the question asks for palindrome partitions. So, we make partitions(by taking substring of pivot and i)
recursively at each i and only want to explore/recurse it further if the substring(first part of partition)
is a palindrome.This way, we prune the invalid paths.We iteratively recurse until our pivot goes out of
bounds and then we add it to our result set.
*/
class Solution {
List<List<String>> result;
public List<List<String>> partition(String s) {
this.result = new ArrayList<>();
helper(s, 0, new ArrayList<>());
return result;
}

private void helper(String s, int pivot, List<String> path) {
if(pivot == s.length())
result.add(new ArrayList<>(path));
for(int i = pivot ; i < s.length() ; i++) {
String subStr = s.substring(pivot, i + 1);
if(isPalindrome(subStr)) {
path.add(subStr);
helper(s, i + 1, path);
path.remove(path.size() - 1);
}
}
}

private boolean isPalindrome(String str) {
int left = 0 , right = str.length() - 1;
while(left < right) {
if(str.charAt(left) != str.charAt(right))
return false;
left++;
right--;
}
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)
// 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
/*
The idea is to use a for loop based recursion approach where we follow the steps of action, recurse
and backtrack.Our task/action is to add each element to the path and recurse it further without choosing
the same element(pivot) as question said no duplicates.We backtrack at every recursion to find the
possible combinations and not include unrequired/duplicate combination in the result set.
*/
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 pivot, List<Integer> path) {
result.add(new ArrayList<>(path));
for(int i = pivot ; i < nums.length ; i++) {
//action
path.add(nums[i]);
//recurse
helper(nums, i + 1, path);
//backtrack
path.remove(path.size() - 1);
}
}
}