|
| 1 | +/** |
| 2 | + * @param {character[][]} board |
| 3 | + * @return {boolean} |
| 4 | + */ |
| 5 | +var isValidSudoku = function (board) { |
| 6 | + const checkRows = (board) => { |
| 7 | + for (let i = 0; i < 9; i++) { |
| 8 | + // console.log("i", i); |
| 9 | + const row = board[i]; |
| 10 | + // console.log("row", row); |
| 11 | + const arr = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]; |
| 12 | + |
| 13 | + for (let j = 0; j < 9; j++) { |
| 14 | + if (row[j] !== ".") { |
| 15 | + // console.log("row-element", row[j]); |
| 16 | + let index = arr.indexOf(row[j]); |
| 17 | + // console.log("row-element-index", index); |
| 18 | + if (index !== -1) { |
| 19 | + arr.splice(index, 1); |
| 20 | + } else { |
| 21 | + return false; |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + return true; |
| 28 | + }; |
| 29 | + const checkColumns = (board) => { |
| 30 | + for (let i = 0; i < 9; i++) { |
| 31 | + // console.log("i", i); |
| 32 | + const arr = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]; |
| 33 | + |
| 34 | + for (let j = 0; j < 9; j++) { |
| 35 | + if (board[j][i] !== ".") { |
| 36 | + // console.log("row-element", row[j]); |
| 37 | + let index = arr.indexOf(board[j][i]); |
| 38 | + // console.log("row-element-index", index); |
| 39 | + if (index !== -1) { |
| 40 | + arr.splice(index, 1); |
| 41 | + } else { |
| 42 | + return false; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + return true; |
| 48 | + }; |
| 49 | + const checkSquares = (board) => { |
| 50 | + const mapInitialCell = (i) => { |
| 51 | + return { |
| 52 | + row: (i % 3) * 3, |
| 53 | + column: Math.floor(i / 3) * 3, |
| 54 | + }; |
| 55 | + }; |
| 56 | + |
| 57 | + const mapRequiredCell = (initialCell, j) => { |
| 58 | + const rowAddition = j % 3; |
| 59 | + const columnAddition = Math.floor(j / 3); |
| 60 | + |
| 61 | + return { |
| 62 | + row: initialCell.row + rowAddition, |
| 63 | + column: initialCell.column + columnAddition, |
| 64 | + }; |
| 65 | + }; |
| 66 | + |
| 67 | + for (let i = 0; i < 9; i++) { |
| 68 | + // console.log("i", i); |
| 69 | + const arr = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]; |
| 70 | + |
| 71 | + const initialCell = mapInitialCell(i); |
| 72 | + |
| 73 | + for (let j = 0; j < 9; j++) { |
| 74 | + const requiredCell = mapRequiredCell(initialCell, j); |
| 75 | + |
| 76 | + if (board[requiredCell.row][requiredCell.column] !== ".") { |
| 77 | + // console.log("row-element", row[j]); |
| 78 | + let index = arr.indexOf( |
| 79 | + board[requiredCell.row][requiredCell.column] |
| 80 | + ); |
| 81 | + // console.log("row-element-index", index); |
| 82 | + if (index !== -1) { |
| 83 | + arr.splice(index, 1); |
| 84 | + } else { |
| 85 | + return false; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + return true; |
| 91 | + }; |
| 92 | + |
| 93 | + if (checkRows(board) && checkColumns(board) && checkSquares(board)) |
| 94 | + return true; |
| 95 | + |
| 96 | + return false; |
| 97 | +}; |
| 98 | + |
| 99 | +const board = [ |
| 100 | + ["5", "3", ".", ".", "7", ".", ".", ".", "."], |
| 101 | + ["6", ".", ".", "1", "9", "5", ".", ".", "."], |
| 102 | + [".", "9", "8", ".", ".", ".", ".", "6", "."], |
| 103 | + ["8", ".", ".", ".", "6", ".", ".", ".", "3"], |
| 104 | + ["4", ".", ".", "8", ".", "3", ".", ".", "1"], |
| 105 | + ["7", ".", ".", ".", "2", ".", "1", ".", "6"], |
| 106 | + [".", "6", ".", ".", ".", ".", "2", "8", "."], |
| 107 | + [".", ".", ".", "4", "1", "9", ".", ".", "5"], |
| 108 | + [".", ".", ".", ".", "8", ".", ".", "7", "9"], |
| 109 | +]; |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +console.log(isValidSudoku(board)); |
0 commit comments