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
32 changes: 32 additions & 0 deletions palindrome_partionining.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

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));
return;
}

for(int i=pivot; i<s.length();i++){
String subStr = s.substring(pivot, i+1);
if(palindrome(subStr)){
path.add(subStr);
helper(s,i+1,path);
path.remove(path.size()-1);
}
}
}
private boolean palindrome(String s){
int left =0, right = s.length()-1;
while(left<right){
if(s.charAt(left)!=s.charAt(right)) return false;
left++;right--;
}
return true;
}
}
46 changes: 46 additions & 0 deletions subsets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 0-1 recursion
// time complexity - O(n × 2ⁿ)
//space complexity - O(n)
// class Solution {
// List<List<Integer>> result;
// public List<List<Integer>> subsets(int[] nums) {
// this.result = new ArrayList<>();
// List<Integer> path = new ArrayList<>();
// helper(nums, 0, path);
// return result;
// }
// private void helper(int[] nums, int i,List<Integer> path){
// //base
// if(i==nums.length)
// {
// result.add(new ArrayList<>(path));
// return;
// }
// //logic
// //0
// helper(nums,i+1,path);
// path.add(nums[i]);
// helper(nums,i+1,path);
// path.remove(path.size()-1);
// }
// }

//Time Complexity: O(n × 2ⁿ)
//Space Complexity: O(n)
class Solution {
List<List<Integer>> result;
public List<List<Integer>> subsets(int[] nums) {
this.result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
helper(nums,path,0);
return result;
}
private void helper(int[] nums,List<Integer> path,int pivot){
result.add(new ArrayList(path));
for(int i=pivot;i<nums.length;i++){
path.add(nums[i]);
helper(nums,path,i+1);
path.remove(path.size()-1);
}
}
}