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
20 changes: 20 additions & 0 deletions Subsets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Time Complexity : O(2^n) where n is the number of elements in the input array. This is because for each element, we have two choices: include it in a subset or exclude it.
// Space Complexity : O(n) where n is the number of elements in the input array. This is the space required to store all possible subsets.
Comment on lines +1 to +2
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : None
// Your code here along with comments explaining your approach : subsets are generated iteratively by starting with an empty subset and for each number in the input array, we create new subsets by adding that number to all existing subsets. This way, we build up all possible combinations of subsets.
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;
}
}
40 changes: 40 additions & 0 deletions palindromepartition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time Complexity : O(2^n) where n is the length of the input string. This is because for each character in the string, we have two choices: either to include it in the current partition or not, leading to a binary tree of possibilities.
// Space Complexity : O(n) where n is the length of the input string. This is the space required to store the recursion stack and the resulting partitions.
Comment on lines +1 to +2
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : Understanding the backtracking approach for generating all possible partitions.
// Your code here along with comments explaining your approach : used backtracking to generate all possible partitions of the input string. For each substring, we check if it is a palindrome. If it is, we add it to the current path and recursively call the helper function with the remaining substring. When we reach the end of the string, we add the current path to the result list.
class Solution {
List<List<String>> result;
public List<List<String>> partition(String s) {
this.result = new ArrayList<>();
helper(s, new ArrayList<>());
return result;
}

private void helper(String s, List<String> path){
if(s.length() == 0){
result.add(new ArrayList<>(path));
return;
}
for(int i=0; i<s.length(); i++){
String subStr = s.substring(0, i+1);
if(isPalindrome(subStr)){
path.add(subStr);
helper(s.substring(i+1), path);
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;
}
}