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
47 changes: 47 additions & 0 deletions PalindromePartition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Time Complexity : O(L.2^L). L is the length of the input string
Space Complexity : O(L), L characters in the recursion stack
Did this code successfully run on Leetcode : yes
Any problem you faced while coding this : No
Approach :

We need to partition the string, we can do that by creating substring between 2 indices(pivot and index).
We need to check if all the substrings is valid palindrome, if yes, add the substrings to a result array.

*/
import java.util.ArrayList;
import java.util.List;

public class PalindromePartition {

public List<List<String>> partition(String s) {
List<List<String>> result = new ArrayList<>();
partitionRecursion(s, result, new ArrayList<>(), 0);
return result;
}

private void partitionRecursion(String s, List<List<String>> result, List<String> path, int pivot) {
if(pivot == s.length()){
result.add(new ArrayList<>(path));
return;
}

for (int i = pivot; i < s.length(); i++) {
String subString = s.substring(pivot, i+1);
if(isPalindrome(subString)){
path.add(subString);
partitionRecursion(s, result, path, i+1);
// backtrack
path.remove(path.size()-1);
}
}
}

boolean isPalindrome(String s){
int len = s.length();
for (int i = 0; i < len / 2; i++) {
if(s.charAt(i) != s.charAt(len-1-i)) return false;
}
return true;
}
}
48 changes: 48 additions & 0 deletions Subsets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Time Complexity : O(2^N), nums array size
Space Complexity : O(N), in the recursion stack
Did this code successfully run on Leetcode : Yes
Any problem you faced while coding this : No
Approach :
We need to find all combinations of the numbers in the input array. To find combination, we can either do choose and not choose
recursion, 0/1 recursion or for loop based recursion. In either of the recursion, we need to maintain a path, that would store the current
combination, once we are done visiting a number we can remove it. As in, we need to back track the last value added to the path.
This is because we are using same path object to create all paths, if we want to avoid backtracking, we need to create a new array
for each path. As this is not space efficient, we rather choose to back track.
*/
import java.util.ArrayList;
import java.util.List;

public class Subsets {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
getSubsets(nums, 0,new ArrayList<>(), result);
// getSubsets01Recursion(nums, 0, new ArrayList<>(), result);
return result;
}

private void getSubsets01Recursion(int[] nums, int index, List<Integer> path, List<List<Integer>> result) {
if(index == nums.length){
result.add(new ArrayList<>(path));
return;
}

path.add(nums[index]);
getSubsets01Recursion(nums, index+1, path, result);
path.remove(path.size()-1);

getSubsets01Recursion(nums, index+1, path, result);
}

// Time Complexity : O(N * 2^N), Extra O(N) as we are creating deep copy of path and in worst case, path can have N values
// Space Complexity : O(N)
private void getSubsets(int[] nums, int pivot, List<Integer> path, List<List<Integer>> result) {
result.add(new ArrayList<>(path));

for (int i = pivot; i < nums.length; i++){
path.add(nums[i]);
getSubsets(nums, i+1, path, result);
path.remove(path.size()-1);
}
}
}