Category : backtracking

Im trying to make a sudoku solver algorithm to practice recursion and backtracking, so made this function ‘backtrack’ which is the main function that’s supposed to solve it , and four other ‘utility/helper’ functions;the first one is ‘getSubGrid’ which returns the subgrid where the current number belongs,and then ‘getColumn’ which returns the column where the ..

Read more

In trying to solve the following problem: Generate all combinations of an array of string. Ex: Input: [‘A’, ‘T’, ‘C’, ‘K’] Output: [ ‘ATCK’, ‘ATC’, ‘ATK’, ‘AT’, ‘ACK’, ‘AC’, ‘AK’, ‘A’, ‘TCK’, ‘TC’, ‘TK’, ‘T’, ‘CK’, ‘C’, ‘K’, ” ] I have the following code: function getSpellableWords(arr) { const res = []; // helper(arr, res, ..

Read more

let row = this.findEmpty(puzzleString)[0]; let col = this.findEmpty(puzzleString)[1]; let i = this.findEmpty(puzzleString)[2]; if(!this.findEmpty(puzzleString)) return puzzleString for(let num = 1; num < 10; num++){ if(this.checkValue(puzzleString, row, col, num)){ puzzleString[i] = num; this.solve(puzzleString) } } findEmpty(puzzleString) iterates over the puzzle string and returns the row (A-I), column (1-9), and index of a blank grid. checkValue() contains 3 ..

Read more

On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square.  There is exactly one starting square. 2 represents the ending square.  There is exactly one ending square. 0 represents empty squares we can walk over. -1 represents obstacles that we cannot walk over. Return the number of 4-directional walks from the starting ..

Read more