Created
February 3, 2020 14:34
-
-
Save spurscho/6488f12bf5ccbf4da208ea3de323a98a to your computer and use it in GitHub Desktop.
36. Valid Sudoku
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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