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
23 changes: 23 additions & 0 deletions Problem-1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Time Complexity: O(n * 2^n), n = length of input array, because each element can double the number of subsets
// Space Complexity: O(2^n), to store all subsets
// 1. Start with an empty subset and iteratively build all subsets by adding each number to existing subsets.
// 2. For each number, copy all existing subsets, add the current number, and append to the result.
// 3. At the end, the result contains all possible subsets of the input array.

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

for (int num : nums) {
int resultSize = result.size();
for (int j = 0; j < resultSize; j++) {
List<Integer> temp = new ArrayList<>(result.get(j)); // make a copy
temp.add(num); // add current number
result.add(temp);
}
}

return result;
}
}
19 changes: 19 additions & 0 deletions Problem-1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Time Complexity: O(n * 2^n), n = length of input array, because each element can double the number of subsets
# Space Complexity: O(2^n), to store all subsets
# 1. Start with an empty subset and iteratively build all subsets by adding each number to existing subsets.
# 2. For each number, copy all existing subsets, add the current number, and append to the result.
# 3. At the end, the result contains all possible subsets of the input array.

class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
result = []
result.append([])
for i in range(len(nums)):
result_size = len(result)
for j in range(result_size):
temp = result[j].copy()
temp.append(nums[i])
result.append(temp)
print(result)
return result

41 changes: 41 additions & 0 deletions Problem-2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Time Complexity: O(n * 2^n) in the worst case, n = length of the string
// Space Complexity: O(n) for recursion stack + O(total partitions) for result
// 1. Use DFS/backtracking to explore all possible partitions of the string.
// 2. For each substring, check if it is a palindrome before adding to the current path.
// 3. Add the current path to the result when the end of the string is reached and backtrack.

class Solution {
List<List<String>> result;

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

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

for (int i = pivot; i < s.length(); i++) {
String curr = s.substring(pivot, i + 1);
if (isPalindrome(curr)) {
path.add(curr);
helper(s, path, i + 1);
path.remove(path.size() - 1); // backtrack
}
}
}

private boolean isPalindrome(String curr) {
int i = 0, j = curr.length() - 1;
while (i < j) {
if (curr.charAt(i) != curr.charAt(j)) return false;
i++;
j--;
}
return true;
}
}
31 changes: 31 additions & 0 deletions Problem-2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Time Complexity: O(n * 2^n) in the worst case, n = length of the string
# Space Complexity: O(n) for recursion stack + O(total partitions) for result
# 1. Use DFS/backtracking to explore all possible partitions of the string.
# 2. For each substring, check if it is a palindrome before adding to the current path.
# 3. Add the current path to the result when the end of the string is reached and backtrack.

class Solution:
def partition(self, s: str) -> List[List[str]]:
self.result = []
self.helper(s,[],0)
return self.result
def helper(self,s,path,pivot):
if pivot == len(s):
self.result.append(list(path))
return
for i in range(pivot, len(s)):
curr = s[pivot:i+1]
if self.isPalindrome(curr):
path.append(curr)
self.helper(s,path,i+1)
path.pop()
def isPalindrome(self,curr):
i = 0
j = len(curr)-1
while i < j:
if curr[i] != curr[j]:
return False
i += 1
j -= 1
return True