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
68 changes: 68 additions & 0 deletions PalindromePartitioning.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import java.util.ArrayList;
import java.util.List;

/**
* Palindrome Partitioning
* LeetCode: https://leetcode.com/problems/palindrome-partitioning/
*
* Approach:
* - Uses backtracking to explore all possible partitions of the input string.
* - At each step, it checks if the current substring is a palindrome.
* - If it is, it adds the substring to the current path and recursively partitions the remaining string.
* - When the end of the string is reached, the current path is added to the result.
* - Backtracking ensures all possible partitions are explored.
*
* Time Complexity: O(N * 2^N)
* - There are 2^(N-1) possible ways to partition a string of length N.
* - For each partition, checking if a substring is a palindrome takes O(N) time in the worst case.
* - So, overall complexity is O(N * 2^N).
*
* Space Complexity: O(N * 2^N)
* - The result list can contain up to 2^(N-1) partitions, each with up to N substrings.
* - The recursion stack can go up to N levels deep.
*/
public class PalindromePartitioning {

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 current = s.substring(pivot, i + 1);
if (isPalindrome(current)) {
//action
path.add(current);
//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;
i++;
j--;
}
return true;
}

public static void main(String[] args) {
System.out.println(new PalindromePartitioning().partition("aabbb"));
}
}
68 changes: 68 additions & 0 deletions PalindromePartitioning2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import java.util.ArrayList;
import java.util.List;

/**
* Palindrome Partitioning (Variant)
* LeetCode: https://leetcode.com/problems/palindrome-partitioning/
*
* Approach:
* - Uses backtracking to explore all possible partitions of the input string.
* - Tracks the count of characters included in the current path to ensure full coverage.
* - At each step, checks if the current substring is a palindrome and recursively partitions the remaining string.
* - Backtracking ensures all possible partitions are explored.
*
* Time Complexity: O(N * 2^N)
* - There are 2^(N-1) possible ways to partition a string of length N.
* - For each partition, checking if a substring is a palindrome takes O(N) time in the worst case.
* - So, overall complexity is O(N * 2^N).
*
* Space Complexity: O(N * 2^N)
* - The result list can contain up to 2^(N-1) partitions, each with up to N substrings.
* - The recursion stack can go up to N levels deep.
*/

public class PalindromePartitioning2 {

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

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

helper(s, povit, idx + 1, count, path, result);
String current = s.substring(povit, idx+1);
if (isPalindrome(current)) {
count += current.length();
//action
path.add(current);
//recursion
helper(s, idx + 1, idx + 1, count, path, result);
//backtrack
path.removeLast();
}
}

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;
i++;
j--;
}
return true;
}

public static void main(String[] args) {
System.out.println(new PalindromePartitioning2().partition("aab"));
}
}
43 changes: 43 additions & 0 deletions Subsets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Subsets (Backtracking)
* LeetCode: https://leetcode.com/problems/subsets/
*
* Approach:
* - Uses backtracking to generate all possible subsets of the input array.
* - At each step, chooses to include or exclude the current element.
* - Recursively explores both choices and adds each subset to the result.
*
* Time Complexity: O(N * 2^N)
* - There are 2^N possible subsets for an array of length N.
* - Each subset can take up to O(N) time to copy.
*
* Space Complexity: O(N * 2^N)
* - The result list contains 2^N subsets, each with up to N elements.
* - The recursion stack can go up to N levels deep.
*/

import java.util.ArrayList;
import java.util.List;

public class Subsets {

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 idx, List<Integer> path, List<List<Integer>> result) {
//base
if (idx == nums.length) {
result.add(new ArrayList<>(path));
return;
}
//logic
helper(nums, idx + 1, path, result);

path.add(nums[idx]);
helper(nums, idx + 1, path, result);
path.removeLast();
}
}
48 changes: 48 additions & 0 deletions Subsets2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.ArrayList;
import java.util.List;

/**
* Subsets (Backtracking with Pivot)
* LeetCode: https://leetcode.com/problems/subsets/
*
* Approach:
* - Uses backtracking and a pivot index to generate all possible subsets.
* - At each recursive call, adds the current path to the result.
* - Iterates from the pivot index, adds each element to the path, and recurses.
* - Backtracks after each recursion to explore all combinations.
*
* Time Complexity: O(N * 2^N)
* - There are 2^N possible subsets for an array of length N.
* - Each subset can take up to O(N) time to copy.
*
* Space Complexity: O(N * 2^N)
* - The result list contains 2^N subsets, each with up to N elements.
* - The recursion stack can go up to N levels deep.
*/

public class Subsets2 {

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) {
result.add(new ArrayList<>(path));
return;
}
result.add(new ArrayList<>(path));
//logic
for (int i = pivot; i < nums.length; i++) {
//action
path.add(nums[i]);
//recurse
helper(nums, i + 1, path, result);
//backtrack
path.remove(path.size() - 1);
}
}
}
35 changes: 35 additions & 0 deletions Subsets3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.ArrayList;
import java.util.List;

/**
* Subsets (Iterative)
* LeetCode: https://leetcode.com/problems/subsets/
*
* Approach:
* - Uses an iterative approach to generate all possible subsets.
* - Starts with an empty subset and iteratively adds each number to existing subsets to form new ones.
* - Efficiently builds the power set without recursion or backtracking.
*
* Time Complexity: O(N * 2^N)
* - There are 2^N possible subsets for an array of length N.
* - Each subset can take up to O(N) time to copy.
*
* Space Complexity: O(N * 2^N)
* - The result list contains 2^N subsets, each with up to N elements.
*/
public class Subsets3 {

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> list = new ArrayList<>(result.get(j));
list.add(nums[i]);
result.add(list);
}
}
return result;
}
}