RecursionProblem
RecursionProblem
Permutations of a String
Problem Statement: Given a string, generate all possible permutations of the characters in the string. A
permutation of a string is an arrangement of its characters into a sequence or linear order. The order of
characters matters.
Input:
Output:
Example:
Input: "abc"
Detailed Description:
The problem requires generating every possible arrangement of the characters in the string. The
function should handle swapping characters recursively to achieve this. Each permutation should be
unique and the total number of permutations should be n!n!n! (factorial of nnn).
2. Combination Sum
Problem Statement: Given an array of distinct integers and a target sum, find all unique combinations of
numbers that sum up to the target. Each number in the array may be used an unlimited number of
times in the combination.
Input:
An integer target.
Output:
A list of lists, where each list is a combination of numbers that add up to the target.
Example:
The function needs to find combinations of numbers from the candidates array that sum up to the
target. Each number in the array can be used multiple times. The function uses backtracking to explore
all possible combinations, ensuring that each combination is unique.
3. Generate Parentheses
Problem Statement: Given nnn pairs of parentheses, write a function to generate all combinations of
well-formed parentheses.
Input:
Output:
A list of strings, where each string represents a valid combination of nnn pairs of parentheses.
Example:
Input: n = 3
Detailed Description:
The function generates all valid combinations of nnn pairs of parentheses using backtracking. It ensures
that at any point, the number of closing parentheses does not exceed the number of opening
parentheses. The function should return all possible combinations where the parentheses are correctly
balanced.
4. Word Search
Problem Statement: Given a 2D grid of characters and a word, find if the word exists in the grid. The
word can be constructed from letters of sequentially adjacent cells, where "adjacent" cells are
horizontally or vertically neighboring. The same letter cell may not be used more than once.
Input:
A string word.
Output:
A boolean value True if the word exists in the grid, False otherwise.
Example:
Output: True
Detailed Description:
The function searches for the word in the grid using Depth-First Search (DFS). It explores all possible
paths from each cell, marking cells as visited to avoid reuse in the same word path. The function should
return True if the word can be found in the grid and False otherwise.
5. N-Queens Problem
Problem Statement: Place nnn queens on an n×nn \times nn×n chessboard such that no two queens
threaten each other. A queen can attack horizontally, vertically, or diagonally.
Input:
Output:
A list of lists, where each list represents a valid arrangement of nnn queens on the board. Each
string in the list represents a row of the chessboard, with 'Q' indicating a queen and '.' indicating
an empty space.
Example:
Input: n = 4
Output:
css
Copy code
Detailed Description:
The function solves the problem using backtracking. It places queens one by one, checking for conflicts
and backtracking when a conflict is found. The goal is to find all possible arrangements where no two
queens can attack each other. Each solution should be a valid configuration of the chessboard with nnn
queens placed.