diff --git a/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java b/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java
index 269880b8ddae..41d3477f58e1 100644
--- a/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java
+++ b/src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java
@@ -32,6 +32,7 @@
*
This implementation is intended for educational purposes.
*
* @see Depth First Search
+
*/
@SuppressWarnings({"rawtypes", "unchecked"})
diff --git a/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java b/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java
index d05e33a4242f..ced7fe73ab4b 100644
--- a/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java
+++ b/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java
@@ -18,7 +18,12 @@ private ArrayCombination() {
* @param k The desired length of each combination.
* @return A list containing all combinations of length k.
* @throws IllegalArgumentException if n or k are negative, or if k is greater than n.
+ *
+ * Complexity Analysis
+ * Time Complexity: O(C(n, k) * k)
+ * Space Complexity: O(k)
*/
+
public static List> combination(int n, int k) {
if (k < 0 || k > n) {
throw new IllegalArgumentException("Invalid input: 0 ≤ k ≤ n is required.");
diff --git a/src/main/java/com/thealgorithms/backtracking/Combination.java b/src/main/java/com/thealgorithms/backtracking/Combination.java
index 377d2c862d54..2f30259eff1e 100644
--- a/src/main/java/com/thealgorithms/backtracking/Combination.java
+++ b/src/main/java/com/thealgorithms/backtracking/Combination.java
@@ -8,7 +8,18 @@
/**
* Finds all combinations of a given array using backtracking algorithm * @author Alan Piao (git-Alan Piao)
+ *
+ * Complexity Analysis
+ *
+ * Time Complexity:
+ * O(C(N, n) * n log n), where N is array size and n is combination length.
+ * The log n factor comes from TreeSet operations (add/remove).
+ *
+ * Space Complexity:
+ * O(C(N, n) * n) for storing all combinations +
+ * O(n) auxiliary space for recursion and current set.
*/
+
public final class Combination {
private Combination() {
}
diff --git a/src/main/java/com/thealgorithms/backtracking/CombinationSum.java b/src/main/java/com/thealgorithms/backtracking/CombinationSum.java
index 09b99032bdc1..7bfd47f258ad 100644
--- a/src/main/java/com/thealgorithms/backtracking/CombinationSum.java
+++ b/src/main/java/com/thealgorithms/backtracking/CombinationSum.java
@@ -4,7 +4,22 @@
import java.util.Arrays;
import java.util.List;
-/** Backtracking: pick/not-pick with reuse of candidates. */
+/**
+ * Backtracking: pick/not-pick with reuse of candidates.
+ *
+ * Complexity Analysis
+ *
+ * Time Complexity:
+ * O(N^(T / min)), where N is number of candidates,
+ * T is target, and min is the smallest candidate value.
+ * In practice, it is O(P * k), where P is number of valid combinations
+ * and k is average combination length.
+ *
+ * Space Complexity:
+ * O(T / min) for recursion stack and current combination +
+ * O(P * (T / min)) for storing all results.
+ */
+
public final class CombinationSum {
private CombinationSum() {
throw new UnsupportedOperationException("Utility class");
diff --git a/src/main/java/com/thealgorithms/backtracking/CrosswordSolver.java b/src/main/java/com/thealgorithms/backtracking/CrosswordSolver.java
index 6bfb026c7de9..97e5f7c81bc3 100644
--- a/src/main/java/com/thealgorithms/backtracking/CrosswordSolver.java
+++ b/src/main/java/com/thealgorithms/backtracking/CrosswordSolver.java
@@ -8,20 +8,32 @@
* A class to solve a crossword puzzle using backtracking.
* Example:
* Input:
- * puzzle = {
- * {' ', ' ', ' '},
- * {' ', ' ', ' '},
- * {' ', ' ', ' '}
- * }
- * words = List.of("cat", "dog")
+ * puzzle = {
+ * {' ', ' ', ' '},
+ * {' ', ' ', ' '},
+ * {' ', ' ', ' '}
+ * }
+ * words = List.of("cat", "dog")
*
* Output:
- * {
- * {'c', 'a', 't'},
- * {' ', ' ', ' '},
- * {'d', 'o', 'g'}
- * }
+ * {
+ * {'c', 'a', 't'},
+ * {' ', ' ', ' '},
+ * {'d', 'o', 'g'}
+ * }
+ *
+ * Complexity Analysis
+ *
+ * Time Complexity:
+ * O((2W)^W * L), where W is number of words and L is average word length.
+ * In the worst case, this can be approximated as O(W! * 2^W * L)
+ * due to trying all permutations of words with two placement directions.
+ *
+ * Space Complexity:
+ * O(W) for recursion stack +
+ * O(R * C) for the puzzle grid.
*/
+
public final class CrosswordSolver {
private CrosswordSolver() {
}
diff --git a/src/main/java/com/thealgorithms/backtracking/FloodFill.java b/src/main/java/com/thealgorithms/backtracking/FloodFill.java
index 0f31a9c5a30e..8e2fec715bf0 100644
--- a/src/main/java/com/thealgorithms/backtracking/FloodFill.java
+++ b/src/main/java/com/thealgorithms/backtracking/FloodFill.java
@@ -14,6 +14,15 @@ private FloodFill() {
* @param image The image to be filled
* @param x The x coordinate of which color is to be obtained
* @param y The y coordinate of which color is to be obtained
+ *
+ * Complexity Analysis:
+ *
+ * Time Complexity:
+ * O(R * C), where R is number of rows and C is number of columns.
+ * Each cell is visited at most once.
+ *
+ * Space Complexity:
+ * O(R * C) in the worst case due to recursion stack.
*/
public static int getPixel(final int[][] image, final int x, final int y) {
diff --git a/src/main/java/com/thealgorithms/backtracking/KnightsTour.java b/src/main/java/com/thealgorithms/backtracking/KnightsTour.java
index 2c2da659f3aa..4027bd478345 100644
--- a/src/main/java/com/thealgorithms/backtracking/KnightsTour.java
+++ b/src/main/java/com/thealgorithms/backtracking/KnightsTour.java
@@ -1,5 +1,6 @@
package com.thealgorithms.backtracking;
+import java.sql.Time;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
@@ -66,6 +67,18 @@ public static void resetBoard() {
* @param count The current move number
* @return True if a solution is found, False otherwise
*/
+
+ /**
+ * Time Complexity:
+ *
+ * Worst Case: O(8^(N^2)) due to backtracking.
+ * Practical Complexity: Significantly reduced to near O(N^2)
+ * using Warnsdorff’s heuristic and pruning (orphan detection).
+ *
+ * Space Complexity:
+ * O(N^2) for the board and recursion stack.
+ */
+
static boolean solve(int row, int column, int count) {
if (count > total) {
return true;
diff --git a/src/main/java/com/thealgorithms/backtracking/MColoring.java b/src/main/java/com/thealgorithms/backtracking/MColoring.java
index d0188dfd13aa..8ebbb13db0e6 100644
--- a/src/main/java/com/thealgorithms/backtracking/MColoring.java
+++ b/src/main/java/com/thealgorithms/backtracking/MColoring.java
@@ -35,6 +35,16 @@ private MColoring() {
* @param m The maximum number of allowed colors.
* @return true if the graph can be colored using M colors, false otherwise.
*/
+
+ /**
+ * Time Complexity:
+ * O(V + E), where V is number of vertices and E is number of edges.
+ * Each node and edge is processed once during BFS traversal.
+ *
+ * Space Complexity:
+ * O(V + E) for storing the graph and BFS queue.
+ */
+
static boolean isColoringPossible(ArrayList nodes, int n, int m) {
// Visited array keeps track of whether each node has been processed.
diff --git a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java
index 8247172e7ee0..31b70a356379 100644
--- a/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java
+++ b/src/main/java/com/thealgorithms/backtracking/MazeRecursion.java
@@ -50,6 +50,18 @@ public static int[][] solveMazeUsingSecondStrategy(int[][] map) {
* @param j The current y-coordinate of the ball (column index)
* @return True if a path is found to (6,5), otherwise false
*/
+
+ /**
+ * Complexity Analysis
+ *
+ * Time Complexity:
+ * O(R * C), where R is number of rows and C is number of columns.
+ * Each cell is visited at most once.
+ *
+ * Space Complexity:
+ * O(R * C) in the worst case due to recursion stack.
+ */
+
private static boolean setWay(int[][] map, int i, int j) {
if (map[6][5] == 2) {
return true;
diff --git a/src/main/java/com/thealgorithms/backtracking/NQueens.java b/src/main/java/com/thealgorithms/backtracking/NQueens.java
index 1a8e453e34cb..b06ac73357bb 100644
--- a/src/main/java/com/thealgorithms/backtracking/NQueens.java
+++ b/src/main/java/com/thealgorithms/backtracking/NQueens.java
@@ -64,6 +64,21 @@ public static void placeQueens(final int queens) {
* @param columns: columns[i] = rowId where queen is placed in ith column.
* @param columnIndex: This is the column in which queen is being placed
*/
+
+ /**
+ * Complexity Analysis:
+ *
+ * Time Complexity:
+ * O(N! * N), where N is the number of queens.
+ * The factorial comes from placing queens column by column,
+ * and O(N) is for checking validity at each step.
+ *
+ * Space Complexity:
+ * O(N) for recursion stack and column tracking +
+ * O(S * N^2) for storing all valid solutions,
+ * where S is the number of solutions.
+ */
+
private static void getSolution(int boardSize, List> solutions, int[] columns, int columnIndex) {
if (columnIndex == boardSize) {
// this means that all queens have been placed
diff --git a/src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java b/src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java
index bf93f946ab7b..aaee2f3a74a1 100644
--- a/src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java
+++ b/src/main/java/com/thealgorithms/backtracking/ParenthesesGenerator.java
@@ -35,6 +35,18 @@ public static List generateParentheses(final int n) {
* @param close The number of closed parentheses.
* @param n The total number of pairs of parentheses.
*/
+
+ /**
+ * Complexity Analysis
+ *
+ * Time Complexity:
+ * O(Cn * n), where Cn is the nth Catalan number
+ * (approximately 4^n / √n). Each valid combination takes O(n) time to build.
+ *
+ * Space Complexity:
+ * O(Cn * n) for storing all combinations +
+ * O(n) recursion stack.
+ */
private static void generateParenthesesHelper(List result, final String current, final int open, final int close, final int n) {
if (current.length() == n * 2) {
result.add(current);
diff --git a/src/main/java/com/thealgorithms/backtracking/Permutation.java b/src/main/java/com/thealgorithms/backtracking/Permutation.java
index 21d26e53980f..b136738b01f0 100644
--- a/src/main/java/com/thealgorithms/backtracking/Permutation.java
+++ b/src/main/java/com/thealgorithms/backtracking/Permutation.java
@@ -31,6 +31,19 @@ public static List permutation(T[] arr) {
* @param result the list contains all permutations.
* @param the type of elements in the array.
*/
+
+ /**
+ * Complexity Analysis
+ *
+ * Time Complexity:
+ * O(N! * N), where N is the size of the array.
+ * There are N! permutations and each takes O(N) time to copy.
+ *
+ * Space Complexity:
+ * O(N! * N) for storing all permutations +
+ * O(N) recursion stack.
+ */
+
private static void backtracking(T[] arr, int index, List result) {
if (index == arr.length) {
result.add(arr.clone());
diff --git a/src/main/java/com/thealgorithms/backtracking/PowerSum.java b/src/main/java/com/thealgorithms/backtracking/PowerSum.java
index b34ba660ebd7..406e09fee0d7 100644
--- a/src/main/java/com/thealgorithms/backtracking/PowerSum.java
+++ b/src/main/java/com/thealgorithms/backtracking/PowerSum.java
@@ -36,6 +36,16 @@ public int powSum(int targetSum, int power) {
* @param currentSum The current sum of powered numbers
* @return The number of valid combinations
*/
+
+ /**
+ * Time Complexity:
+ * O(2^M), where M ≈ N^(1/X).
+ * This corresponds to exploring all subsets of numbers whose Xth power is ≤ N.
+ *
+ * Space Complexity:
+ * O(M) for recursion stack.
+ */
+
private int sumRecursive(int remainingSum, int power, int currentNumber, int currentSum) {
int newSum = currentSum + (int) Math.pow(currentNumber, power);
diff --git a/src/main/java/com/thealgorithms/backtracking/SubsequenceFinder.java b/src/main/java/com/thealgorithms/backtracking/SubsequenceFinder.java
index 4a159dbfe0b1..2b8646911009 100644
--- a/src/main/java/com/thealgorithms/backtracking/SubsequenceFinder.java
+++ b/src/main/java/com/thealgorithms/backtracking/SubsequenceFinder.java
@@ -39,6 +39,19 @@ public static List> generateAll(List sequence) {
* @param allSubSequences contains all sequences
* @param the type of elements which we generate
*/
+
+ /**
+ * Complexity Analysis
+ *
+ * Time Complexity:
+ * O(2^N * N), where N is the size of the input list.
+ * There are 2^N subsequences and each takes O(N) time to copy.
+ *
+ * Space Complexity:
+ * O(2^N * N) for storing all subsequences +
+ * O(N) recursion stack.
+ */
+
private static void backtrack(List sequence, List currentSubsequence, final int index, List> allSubSequences) {
assert index <= sequence.size();
if (index == sequence.size()) {
diff --git a/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java b/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java
index 543fe2d02b50..4a7dacf9ae3a 100644
--- a/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java
+++ b/src/main/java/com/thealgorithms/backtracking/SudokuSolver.java
@@ -42,6 +42,19 @@ public static boolean solveSudoku(int[][] board) {
* @param board the Sudoku board
* @return true if solution is found, false otherwise
*/
+
+ /**
+ * Complexity Analysis
+ *
+ * Time Complexity:
+ * O(9^N), where N is the number of empty cells.
+ * Each empty cell can take values from 1 to 9, leading to exponential
+ * complexity.
+ *
+ * Space Complexity:
+ * O(N) for recursion stack.
+ */
+
private static boolean solve(int[][] board) {
for (int row = 0; row < GRID_SIZE; row++) {
for (int col = 0; col < GRID_SIZE; col++) {
diff --git a/src/main/java/com/thealgorithms/backtracking/UniquePermutation.java b/src/main/java/com/thealgorithms/backtracking/UniquePermutation.java
index 4804e247ab03..2b85e1fa82fb 100644
--- a/src/main/java/com/thealgorithms/backtracking/UniquePermutation.java
+++ b/src/main/java/com/thealgorithms/backtracking/UniquePermutation.java
@@ -11,8 +11,19 @@
* Input: "AAB"
* Output: ["AAB", "ABA", "BAA"]
*
- * Time Complexity: O(n! * n)
+ * Complexity Analysis
+ *
+ * Time Complexity:
+ * O(n! * n)
+ * - n! permutations in worst case
+ * - Each permutation takes O(n) to build
+ *
+ * Space Complexity:
+ * O(n! * n)
+ * - Result list stores all permutations
+ * - Auxiliary space: O(n) for recursion, used array, and StringBuilder
*/
+
public final class UniquePermutation {
private UniquePermutation() {
diff --git a/src/main/java/com/thealgorithms/backtracking/WordPatternMatcher.java b/src/main/java/com/thealgorithms/backtracking/WordPatternMatcher.java
index 1854cab20a7f..44d3eefef815 100644
--- a/src/main/java/com/thealgorithms/backtracking/WordPatternMatcher.java
+++ b/src/main/java/com/thealgorithms/backtracking/WordPatternMatcher.java
@@ -18,7 +18,22 @@
* Pattern: "aabb"
* Input String: "JavaPythonPythonJava"
* Output: false
+ *
+ * Complexity Analysis
+ *
+ * Time Complexity:
+ * O(n^m * n)
+ * - n = length of input string
+ * - m = length of pattern
+ * - For each pattern character, we try all possible substrings
+ * - Additional O(n) cost for substring and matching operations
+ *
+ * Space Complexity:
+ * O(m + n)
+ * - O(m) recursion depth and pattern map
+ * - O(n) for substring mappings in strMap
*/
+
public final class WordPatternMatcher {
private WordPatternMatcher() {
}