Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

RecursionProblem

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

RecursionProblem

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

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:

 A single string sss of length nnn.

Output:

 A list of all permutations of the string sss.

Example:

 Input: "abc"

 Output: ["abc", "acb", "bac", "bca", "cab", "cba"]

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 array of integers candidates.

 An integer target.

Output:

 A list of lists, where each list is a combination of numbers that add up to the target.

Example:

 Input: candidates = [2, 3, 6, 7], target = 7

 Output: [[2, 2, 3], [7]]


Detailed Description:

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:

 An integer nnn representing the number of pairs of parentheses.

Output:

 A list of strings, where each string represents a valid combination of nnn pairs of parentheses.

Example:

 Input: n = 3

 Output: ["((()))", "(()())", "(())()", "()(())", "()()()"]

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 2D list board representing the grid of characters.

 A string word.

Output:
 A boolean value True if the word exists in the grid, False otherwise.

Example:

 Input: board = [['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E']], word = "ABCCED"

 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:

 An integer nnn representing the size of the chessboard.

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

[ [".Q..", "...Q", "Q...", "..Q."],

["..Q.", "Q...", "...Q", ".Q.."]

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.

You might also like