Skip to content

Instantly share code, notes, and snippets.

@spurscho
Created February 3, 2020 14:34
Show Gist options
  • Save spurscho/6488f12bf5ccbf4da208ea3de323a98a to your computer and use it in GitHub Desktop.
Save spurscho/6488f12bf5ccbf4da208ea3de323a98a to your computer and use it in GitHub Desktop.
36. Valid Sudoku
class Solution {
func isValidSudoku(_ board: [[Character]]) -> Bool {
var rowSet = [Set<Character>](repeating: Set<Character>(), count: 9)
var colSet = [Set<Character>](repeating: Set<Character>(), count: 9)
var squareSet = [Set<Character>](repeating: Set<Character>(), count: 9)
for (rowIndex, row) in board.enumerated() {
for (colIndex, character) in row.enumerated() {
if character == Character(".") {
continue
}
guard !rowSet[rowIndex].contains(character) else { // 행별 값 포함여부
return false
}
rowSet[rowIndex].insert(character)
guard !colSet[colIndex].contains(character) else { // 열별 값 포함여부
return false
}
colSet[colIndex].insert(character)
let squareIndex = (rowIndex / 3) * 3 + (colIndex / 3)
guard !squareSet[squareIndex].contains(character) else {
return false
}
squareSet[squareIndex].insert(character)
}
}
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment