CodeWars: Sudoku Solution Validator Solution

Three checks

Conduct 3 checks

• row check
• column check
• square check

Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}function validSolution(board) {2  const set = new Set()3
4  // horizontal check5  for (let row = 0; row < 9; row++) {6    for (let col = 0; col < 9; col++) {7      set.add(board[row][col])8    }9
10    if (set.size !== 9) return false11    set.clear()12  }13
14  // vertical check15  for (let col = 0; col < 9; col++) {16    for (let row = 0; row < 9; row++) {17      set.add(board[row][col])18    }19
20    if (set.size !== 9) return false21    set.clear()22  }23
24  // square check25  for (let row = 0; row < 9; row += 3) {26    for (let col = 0; col < 9; col += 3) {27      for (let i = 0; i < 3; i++) {28        for (let j = 0; j < 3; j++) {29          set.add(board[row + i][col + j])30        }31      }32
33      if (set.size !== 9) return false34      set.clear()35    }36  }37
38  return true39}```

Original problem

codewars

array

matrix

Next Post

Sep 15, 2022

Could work with string instead of using math

Previous Post

LeetCode: Search in Rotated Sorted Array

Sep 12, 2022

Time-boxing and other solutions make life easier

Search Posts