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
26 changes: 26 additions & 0 deletions Problem72.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Time Complexity : O(n × 2ⁿ) - 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) {

if (i == nums.length) {
result.add(new ArrayList<>(path));
return;
}

helper(nums, i + 1, path);

path.add(nums[i]);
helper(nums, i + 1, path);
path.remove(path.size() - 1);
}
}
40 changes: 40 additions & 0 deletions Problem73.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time Complexity : O(2^n * n)
// Space Complexity : O(n^2)

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

//logic
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, result);
path.remove(path.size()-1);
}
}
}

private boolean isPalindrome(String s) {
int left = 0; int right = s.length()-1;
while(left < right) {
if(s.charAt(left) != s.charAt(right)) {
return false;
}
left++; right--;
}

return true;
}
}