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

/*
* Approach
* here are we using recursion to solve the problem,
* basically at each step we have to choose between pick or not pick a number
* for this we are using recurion
*
* we are going to use the for loop based recursion and we are using backtrack clean up path
* and we will make a deepcopy of the path when pointer it to the result
* so that we avoid return null result.
*/
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
helper(nums, 0, new ArrayList<>(), result);
return result;
}

private void helper(int[] nums, int pivot, List<Integer> path, List<List<Integer>> result) {
// base
if (pivot > nums.length) {
return;
}

// logic
result.add(new ArrayList<>(path));
for (int i = pivot; i < nums.length; i++) {
path.add(nums[i]);
helper(nums, i + 1, path, result);

path.remove(path.size() - 1);

}

}
}
64 changes: 64 additions & 0 deletions program2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Time Complexity : O(2^n)
// Space Complexity :O(n)
// Did this code successfully run on Leetcode : yes
// Any problem you faced while coding this :yes

/*
* Approach
* here are we using recursion to solve the problem,
* basically at each step we have to choose between pick or not pick a number
* for this we are using recurion
*
* we are going to use the for loop based recursion
* in each recursive call we start a new for loop from pivot till s len
* create a new sub string from pivot to i+1
* do the palindrome check on the string if true
* add to path make the recursive call again with i+1
* finally backtrack to keep the path update for the cur level
*
*
* for the palindrome check
* we simply
* loop from both ends of the string to see if the are equal of not
*
*/
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 cur = s.substring(pivot, i + 1);
if (isPalindrome(cur)) {
// add
path.add(cur);

// recurse
helper(s, i + 1, path, result);

// backtrack
path.remove(path.size() - 1);
}
}
}

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