LeetCode: Valid Sudoku Solution

Implementation

1function transpose(arrOfArr) {
2 const N = arrOfArr.length
3 const M = arrOfArr[0].length
4 const res = []
5 for (let i = 0; i < M; i++) {
6 let temp = []
7 for (let j = 0; j < N; j++) {
8 temp.push(arrOfArr[j][i])
9 }
10 res.push(temp)
11 }
12 return res
13}
14
15function squareSizeOf3(arrOfArr) {
16 const N = arrOfArr.length
17 const M = arrOfArr[0].length
18 const res = []
19 for (let i = 0; i < N; i += 3) {
20 for (let j = 0; j < M; j += 3) {
21 let temp = []
22 temp.push(arrOfArr[i].slice(j, j + 3))
23 temp.push(arrOfArr[i + 1].slice(j, j + 3))
24 temp.push(arrOfArr[i + 2].slice(j, j + 3))
25 res.push(temp.flat())
26 }
27 }
28 return res
29}
30
31/**
32 * @param {character[][]} board
33 * @return {boolean}
34 */
35var isValidSudoku = function (board) {
36 return [...board, ...transpose(board), ...squareSizeOf3(board)]
37 .map(row => row.filter(cell => cell !== "."))
38 .every(row => row.length === new Set(row).size)
39}

References

Original problem

Similar problems

Sudoku Solver

Check if Every Row and Column Contains All Numbers

Comments

Loading comments...

Tags

leetcode

neetcode

array

hash table

Next Post

LeetCode: Reverse String

Built-in string method

HoningJS

Search Posts