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
46 changes: 46 additions & 0 deletions isPalandome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Time Complexity : O(2^n * n)
// Space Complexity : O(n^2)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : None

class Solution {
List<List<String>> result;
public List<List<String>> partition(String s) {
this.result = new ArrayList<>();
helper(s, 0, 0, 0, new ArrayList<>());
return result;
}

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

//no choose
helper(s, pivot, i+1, size, path);

//choose
String subStr = s.substring(pivot, i+1);
if(isPalindrome(subStr)){
//action
path.add(subStr);
//recurse
helper(s, i+1, i+1, size + subStr.length(), path);
//backtrack
path.remove(path.size()-1);
}
}

private boolean isPalindrome(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;
}
}
22 changes: 22 additions & 0 deletions subsets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Time Complexity : O(nx2)
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : None

class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
result.add(new ArrayList<>());

for (int i = 0; i < nums.length; i++) {
int size = result.size();
for (int j = 0; j < size; j++) {
List<Integer> temp = new ArrayList<>(result.get(j));
temp.add(nums[i]);
result.add(temp);
}
}

return result;
}
}