Category Archives: Backtracking

Warnsdorff’s algorithm for Knight’s tour problem

Problem : A knight is placed on the first block of an empty board and, moving according to the rules of chess, must visit each square exactly once. Following is an example path followed by Knight to cover all the cells. The below grid represents a chessboard with 8 x 8 cells. Numbers in cells… Read More »

Printing all solutions in N-Queen Problem

The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. For example, following is a solution for 4 Queen problem. The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each… Read More »

Print all palindromic partitions of a string

Given a string s, partition s such that every string of the partition is a palindrome. Return all possible palindrome partitioning of s. Example : Input : s = “bcc” Output : [[“b”, “c”, “c”], [“b”, “cc”]] Input : s = “geeks” Output : [[“g”, “e”, “e”, “k”, “s”], [“g”, “ee”, “k”, “s”]] We have… Read More »

Word Break Problem using Backtracking

Given a valid sentence without any spaces between the words and a dictionary of valid English words, find all possible ways to break the sentence in individual dictionary words. Example Consider the following dictionary { i, like, sam, sung, samsung, mobile, ice, cream, icecream, man, go, mango} Input: “ilikesamsungmobile” Output: i like sam sung mobile… Read More »

Partition of a set into K subsets with equal sum

Given an integer array of N elements, the task is to divide this array into K non-empty subsets such that the sum of elements in every subset is same. All elements of this array should be part of exactly one partition. Examples: Input : arr = [2, 1, 4, 5, 6], K = 3 Output… Read More »

Remove Invalid Parentheses

An expression will be given which can contain open and close parentheses and optionally some characters, No other operator will be there in string. We need to remove minimum number of parentheses to make the input string valid. If more than one valid output are possible removing same number of parentheses then print all such… Read More »

Longest Possible Route in a Matrix with Hurdles

Given an M x N matrix, with a few hurdles arbitrarily placed, calculate the length of longest possible route possible from source to destination within the matrix. We are allowed to move to only adjacent cells which are not hurdles. The route cannot contains any diagonal moves and a location once visited in a particular… Read More »

Match a pattern and String without using regular expressions

Given a string, find out if string follows a given pattern or not without using any regular expressions. Examples: Input: string – GraphTreesGraph pattern – aba Output: a->Graph b->Trees Input: string – GraphGraphGraph pattern – aaa Output: a->Graph Input: string – GeeksforGeeks pattern – GfG Output: G->Geeks f->for Input: string – GeeksforGeeks pattern – GG… Read More »

Find paths from corner cell to middle cell in maze

Given a square maze containing positive numbers, find all paths from a corner cell (any of the extreme four corners) to the middle cell. We can move exactly n steps from a cell in 4 directions i.e. North, East, West and South where n is value of the cell, We can move to mat[i+n][j], mat[i-n][j],… Read More »