Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions leet-code/36-valid-sudoku/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# 36-valid-sudoku

Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

1. Each row must contain the digits 1-9 **without repetition**.
2. Each column must contain the digits 1-9 **without repetition**.
3. Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 **without repetition**.

> **Note**:
>
> - A Sudoku board (partially filled) could be valid but is not necessarily solvable.
> - Only the filled cells need to be validated according to the mentioned rules.

### Example 1

**Input:**

```python
board = [
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
```

**Output:**

```
true
```

### Example 2

**Input:**

```python
board = [
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
```

**Output:**

```
false
```

**Explanation:**
Same as Example 1, except the top left corner is modified to `8`. Since there are two 8's in the top-left 3x3 sub-box, it is invalid.

### Constraints

- `board.length == 9`
- `board[i].length == 9`
- `board[i][j]` is a digit `'1'`-`'9'` or `'.'` (empty cell).
67 changes: 67 additions & 0 deletions leet-code/36-valid-sudoku/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @param {character[][]} board
* @return {boolean}
*/
var isValidSudoku = function(board) {
// store which digits have been seen
let s = new Set()

// Check 3x3 grid
for (let g = 0; g < 9; g++) {
let columnOffset; let rowOffset;
// We can use math to compute this but ran out of time
if (g === 0) { columnOffset = 0; rowOffset = 0 }
if (g === 1) { columnOffset = 3; rowOffset = 0 }
if (g === 2) { columnOffset = 6; rowOffset = 0 }
if (g === 3) { columnOffset = 0; rowOffset = 3 }
if (g === 4) { columnOffset = 3; rowOffset = 3 }
if (g === 5) { columnOffset = 6; rowOffset = 3 }
if (g === 6) { columnOffset = 0; rowOffset = 6 }
if (g === 7) { columnOffset = 3; rowOffset = 6 }
if (g === 8) { columnOffset = 6; rowOffset = 6 }
for (let i = 0; i < 9; i++) {
let colIndex = i % 3
let rowIndex = Math.floor(i/3)
const element = board[rowIndex + rowOffset][colIndex + columnOffset]
if (element === '.') continue
// identified rule violation
if (s.has(element)) return false
s.add(element)
}
// reset for each new grid
s = new Set()
}
// reset for next check
s = new Set()
// Check rows
for (let i = 0; i < 9; i++) {
const row = board[i]
for (let j = 0; j < 9; j++) {
const element = row[j]
if (element === '.') continue;
// identified rule violation
if (s.has(element)) return false
s.add(element)
}
// reset for each row
s = new Set()
}

s = new Set();
// Check columns
for (let i = 0; i < 9; i++) {
// The ith column in each row
for (let j = 0; j < 9; j++) {
const element = board[j][i]
if (element === '.') continue;
// identified rule violation
if(s.has(element)) return false;
s.add(element)
}
//reset for each column
s = new Set()
}

// never found an invalid case
return true;
};