From bc1c0f7e16b9cc918ed967f7beb0493ee7d516e3 Mon Sep 17 00:00:00 2001 From: Jer3myYu Date: Wed, 5 Feb 2025 00:03:02 -0500 Subject: [PATCH 1/6] add: FindAllPermutations --- java/Backtracking/FindAllPermutations.java | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 java/Backtracking/FindAllPermutations.java diff --git a/java/Backtracking/FindAllPermutations.java b/java/Backtracking/FindAllPermutations.java new file mode 100644 index 0000000..cfaf144 --- /dev/null +++ b/java/Backtracking/FindAllPermutations.java @@ -0,0 +1,34 @@ +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class FindAllPermutations { + public List> findAllPermutations(int[] nums) { + List> res = new ArrayList<>(); + backtrack(nums, new ArrayList<>(), new HashSet<>(), res); + return res; + } + + private void backtrack(int[] nums, List candidate, Set used, List> res) { + // If the current candidate is a complete permutation, add it to the + // result. + if (candidate.size() == nums.length) { + res.add(new ArrayList<>(candidate)); + return; + } + for (int num : nums) { + if (!used.contains(num)) { + // Add 'num' to the current permutation and mark it as used. + candidate.add(num); + used.add(num); + // Recursively explore all branches using the updated + // permutation candidate. + backtrack(nums, candidate, used, res); + // Backtrack by reversing the changes made. + candidate.remove(candidate.size() - 1); + used.remove(num); + } + } + } +} From ac3691b053236ef0b7e43e7c9ec997636ef7b405 Mon Sep 17 00:00:00 2001 From: Jer3myYu Date: Wed, 5 Feb 2025 00:08:17 -0500 Subject: [PATCH 2/6] add: FindAllSubsets --- java/Backtracking/FindAllSubsets.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 java/Backtracking/FindAllSubsets.java diff --git a/java/Backtracking/FindAllSubsets.java b/java/Backtracking/FindAllSubsets.java new file mode 100644 index 0000000..50213ba --- /dev/null +++ b/java/Backtracking/FindAllSubsets.java @@ -0,0 +1,27 @@ +import java.util.ArrayList; +import java.util.List; + +public class FindAllSubsets { + public List> findAllSubsets(int[] nums) { + List> res = new ArrayList<>(); + backtrack(0, new ArrayList<>(), nums, res); + return res; + } + + private void backtrack(int i, List currSubset, int[] nums, List> res) { + // Base case: if all elements have been considered, add the + // current subset to the output. + if (i == nums.length) { + res.add(new ArrayList<>(currSubset)); + return; + } + // Include the current element and recursively explore all paths + // that branch from this subset. + currSubset.add(nums[i]); + backtrack(i + 1, currSubset, nums, res); + // Exclude the current element and recursively explore all paths + // that branch from this subset. + currSubset.remove(currSubset.size() - 1); + backtrack(i + 1, currSubset, nums, res); + } +} From 085ab5a49f14886da58bdae4dcb220b138d375f9 Mon Sep 17 00:00:00 2001 From: Jer3myYu Date: Wed, 5 Feb 2025 00:18:46 -0500 Subject: [PATCH 3/6] add: NQueens --- java/Backtracking/NQueens.java | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 java/Backtracking/NQueens.java diff --git a/java/Backtracking/NQueens.java b/java/Backtracking/NQueens.java new file mode 100644 index 0000000..74a3998 --- /dev/null +++ b/java/Backtracking/NQueens.java @@ -0,0 +1,41 @@ +import java.util.HashSet; +import java.util.Set; + +public class NQueens { + int res = 0; + + public int nQueens(int n) { + dfs(0, new HashSet<>(), new HashSet<>(), new HashSet<>(), n); + return res; + } + + private void dfs(int r, Set diagonalsSet, Set antiDiagonalsSet, Set colsSet, int n) { + // Termination condition: If we have reached the end of the rows, + // we've placed all 'n' queens. + if (r == n) { + res++; + return; + } + for (int c = 0; c < n; c++) { + int currDiagonal = r - c; + int currAntiDiagonal = r + c; + // If there are queens on the current column, diagonal or + // anti-diagonal, skip this square. + if (colsSet.contains(c) || diagonalsSet.contains(currDiagonal) || antiDiagonalsSet.contains(currAntiDiagonal)) { + continue; + } + // Place the queen by marking the current column, diagonal, and + // anti−diagonal as occupied. + colsSet.add(c); + diagonalsSet.add(currDiagonal); + antiDiagonalsSet.add(currAntiDiagonal); + // Recursively move to the next row to continue placing queens. + dfs(r + 1, diagonalsSet, antiDiagonalsSet, colsSet, n); + // Backtrack by removing the current column, diagonal, and + // anti−diagonal from the hash sets. + colsSet.remove(c); + diagonalsSet.remove(currDiagonal); + antiDiagonalsSet.remove(currAntiDiagonal); + } + } +} \ No newline at end of file From 767ed0f8f5fc15a777b5d7e337e8e265b62c6ac4 Mon Sep 17 00:00:00 2001 From: Jer3myYu Date: Wed, 5 Feb 2025 00:35:17 -0500 Subject: [PATCH 4/6] add: CombinationsOfSumK --- java/Backtracking/CombinationsOfSumK.java | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 java/Backtracking/CombinationsOfSumK.java diff --git a/java/Backtracking/CombinationsOfSumK.java b/java/Backtracking/CombinationsOfSumK.java new file mode 100644 index 0000000..47d8bfc --- /dev/null +++ b/java/Backtracking/CombinationsOfSumK.java @@ -0,0 +1,33 @@ +import java.util.ArrayList; +import java.util.List; + +public class CombinationsOfSumK { + public List> combinationsOfSumK(int[] nums, int target) { + List> result = new ArrayList<>(); + dfs(new ArrayList<>(), 0, nums, target, result); + return result; + } + + private void dfs(List combination, int startIndex, int[] nums, int target, List> result) { + // Termination condition: If the target is equal to 0, we found a combination + // that sums to 'k'. + if (target == 0) { + result.add(new ArrayList<>(combination)); + return; + } + // Termination condition: If the target is less than 0, no more valid + // combinations can be created by adding it to the current combination. + if (target < 0) { + return; + } + // Starting from start_index, explore all combinations after adding nums[i]. + for (int i = startIndex; i < nums.length; i++) { + // Add the current number to create a new combination. + combination.add(nums[i]); + // Recursively explore all paths that branch from this new combination. + dfs(combination, i, nums, target - nums[i], result); + // Backtrack by removing the number we just added. + combination.remove(combination.size() - 1); + } + } +} From d25a493eadaa9e64883a724bbad59ec946c5a4e2 Mon Sep 17 00:00:00 2001 From: Jer3myYu Date: Wed, 5 Feb 2025 00:35:31 -0500 Subject: [PATCH 5/6] add: PhoneKeypadCombinations --- .../Backtracking/PhoneKeypadCombinations.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 java/Backtracking/PhoneKeypadCombinations.java diff --git a/java/Backtracking/PhoneKeypadCombinations.java b/java/Backtracking/PhoneKeypadCombinations.java new file mode 100644 index 0000000..b77da96 --- /dev/null +++ b/java/Backtracking/PhoneKeypadCombinations.java @@ -0,0 +1,42 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class PhoneKeypadCombinations { + public List phoneKeypadCombinations(String digits) { + Map keypadMap = new HashMap<>(); + keypadMap.put('2', "abc"); + keypadMap.put('3', "def"); + keypadMap.put('4', "ghi"); + keypadMap.put('5', "jkl"); + keypadMap.put('6', "mno"); + keypadMap.put('7', "pqrs"); + keypadMap.put('8', "tuv"); + keypadMap.put('9', "wxyz"); + List result = new ArrayList<>(); + backtrack(0, new ArrayList<>(), digits, keypadMap, result); + return result; + } + + private void backtrack(int i, List currCombination, String digits, Map keypadMap, List result) { + // Termination condition: if all digits have been considered, add the + // current combination to the output list. + if (currCombination.size() == digits.length()) { + StringBuilder sb = new StringBuilder(); + for (char c: currCombination) { + sb.append(c); + } + result.add(sb.toString()); + return; + } + for (char letter: keypadMap.get(digits.charAt(i)).toCharArray()) { + // Add the current letter. + currCombination.add(letter); + // Recursively explore all paths that branch from this combination. + backtrack(i + 1, currCombination, digits, keypadMap, result); + // Backtrack by removing the letter we just added. + currCombination.remove(currCombination.size() - 1); + } + } +} From 9bac83db10810467d375b60dcfb9a4d0fb39638a Mon Sep 17 00:00:00 2001 From: Jer3myYu Date: Wed, 5 Feb 2025 12:30:28 -0500 Subject: [PATCH 6/6] fix: result to res --- java/Backtracking/CombinationsOfSumK.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/java/Backtracking/CombinationsOfSumK.java b/java/Backtracking/CombinationsOfSumK.java index 47d8bfc..83b05b1 100644 --- a/java/Backtracking/CombinationsOfSumK.java +++ b/java/Backtracking/CombinationsOfSumK.java @@ -3,16 +3,16 @@ public class CombinationsOfSumK { public List> combinationsOfSumK(int[] nums, int target) { - List> result = new ArrayList<>(); - dfs(new ArrayList<>(), 0, nums, target, result); - return result; + List> res = new ArrayList<>(); + dfs(new ArrayList<>(), 0, nums, target, res); + return res; } - private void dfs(List combination, int startIndex, int[] nums, int target, List> result) { + private void dfs(List combination, int startIndex, int[] nums, int target, List> res) { // Termination condition: If the target is equal to 0, we found a combination // that sums to 'k'. if (target == 0) { - result.add(new ArrayList<>(combination)); + res.add(new ArrayList<>(combination)); return; } // Termination condition: If the target is less than 0, no more valid @@ -25,7 +25,7 @@ private void dfs(List combination, int startIndex, int[] nums, int targ // Add the current number to create a new combination. combination.add(nums[i]); // Recursively explore all paths that branch from this new combination. - dfs(combination, i, nums, target - nums[i], result); + dfs(combination, i, nums, target - nums[i], res); // Backtrack by removing the number we just added. combination.remove(combination.size() - 1); }