LeetCode: Valid Sudoku Solution
Implementation
1function transpose(arrOfArr) {2 const N = arrOfArr.length3 const M = arrOfArr[0].length4 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 res13}1415function squareSizeOf3(arrOfArr) {16 const N = arrOfArr.length17 const M = arrOfArr[0].length18 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 res29}3031/**32 * @param {character[][]} board33 * @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
Similar problems
Sudoku Solver
Check if Every Row and Column Contains All Numbers
Comments
Loading comments...
Tags
leetcode
neetcode
array
hash table
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
LeetCode: Reverse String
Built-in string method