-
Notifications
You must be signed in to change notification settings - Fork 889
/
ValidSudoku.swift
53 lines (44 loc) · 1.5 KB
/
ValidSudoku.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* Question Link: https://leetcode.com/problems/valid-sudoku/
* Primary idea: Check rows, columns, and single square separately
*
* Time Complexity: O(n^2), Space Complexity: O(n)
*/
class ValidSudoku {
func isValidSudoku(_ board: [[Character]]) -> Bool {
let len = 9
var rowSet = Array(repeating: Set<Character>(), count: len)
var colSet = Array(repeating: Set<Character>(), count: len)
var boxSet = Array(repeating: Set<Character>(), count: len)
for i in 0..<len {
for j in 0..<len {
let currentChar = board[i][j]
if currentChar == "." {
continue
}
// check row
if !isValid(&rowSet[i], currentChar) {
return false
}
// check column
if !isValid(&colSet[j], currentChar) {
return false
}
// check sub-box
let idx = 3 * (i / 3) + j / 3
if !isValid(&boxSet[idx], currentChar) {
return false
}
}
}
return true
}
private func isValid(_ set: inout Set<Character>, _ char: Character) -> Bool {
if set.contains(char) {
return false
} else {
set.insert(char)
return true
}
}
}