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
42 changes: 42 additions & 0 deletions Parttion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Time Complexity: O(n * 2^n)
// Space Complexity: O(n)
// Ran on Leetcode succefully: yes
class Solution {
List<List<String>> ans;
public List<List<String>> partition(String s) {
ans = new ArrayList<>();
// start from pivot = 0
helperPartition(s, 0, new ArrayList<>());
return ans;
}

public void helperPartition(String s, int pivot, List<String> curr) {
// if pivot manages to reach the end then the curr is valid
if (pivot == s.length()) {
ans.add(new ArrayList<>(curr));
return;
}

for ( int i = pivot; i < s.length(); i++) {
// before adding substring check if it is palindrome;
if(isPalindrome(s.substring(pivot, i + 1))) {
curr.add(s.substring(pivot, i + 1));
helperPartition(s, i + 1, curr);
curr.remove(curr.size() - 1);
}
}
}
// Check if a string is palindrome
public boolean isPalindrome(String s) {
int L = 0;
int R = s.length() - 1;
// if left and right meet without violating the condition then its a palindrome
while (L < R) {
if(s.charAt(L++) != s.charAt(R--)) {
return false;
}
}
return true;

}
}
23 changes: 23 additions & 0 deletions Subsets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Time Complexity: O(n * 2^n)
// Space Complexity: O(n)
// Ran on Leetcode succefully: yes
class Solution {
List<List<Integer>> ans;
public List<List<Integer>> subsets(int[] nums) {
ans = new ArrayList<>();
helperSubsets(nums, 0, new ArrayList<>());
return ans;
}

public void helperSubsets(int [] nums, int pivot, List<Integer> list) {
// add deep copy to ans
ans.add(new ArrayList(list));
for(int i = pivot; i < nums.length; i++) {
list.add(nums[i]);
// explore subsets
helperSubsets(nums, i + 1, list);
// backtrack
list.remove(list.size() - 1);
}
}
}