for row in board { var set = Set<Character>() for char in row { guard char != "." else { continue } guard !set.contains(char) else { return false } set.insert(char) } } for col in 0..<9 { var set = Set<Character>() for row in 0..<9 { let char = board[row][col] guard char != "." else { continue } guard !set.contains(char) else { return false } set.insert(char) } } for blockRow in 0..<3 { for blockCol in 0..<3 { var set = Set<Character>() for row in 0..<3 { for col in 0..<3 { let char = board[blockRow*3 + row][blockCol*3 + col] guard char != "." else { continue } guard !set.contains(char) else { return false } set.insert(char) } } } } return true } } Runtime: 119 ms (50%)
for row in board { var set = Set<Character>() for char in row { guard char != "." else { continue } guard !set.contains(char) else { return false } set.insert(char) } } for col in 0..<9 { var set = Set<Character>() for row in 0..<9 { let char = board[row][col] guard char != "." else { continue } guard !set.contains(char) else { return false } set.insert(char) } } for blockRow in 0..<3 { for blockCol in 0..<3 { var set = Set<Character>() for row in 0..<3 { for col in 0..<3 { let char = board[blockRow*3 + row][blockCol*3 + col] guard char != "." else { continue } guard !set.contains(char) else { return false } set.insert(char) } } } } return true } }
for row in board { let checker = Checker() for char in row { guard checker.check(char) else { return false } } } for col in 0..<9 { let checker = Checker() for row in 0..<9 { let char = board[row][col] guard checker.check(char) else { return false } } } for blockRow in 0..<3 { for blockCol in 0..<3 { let checker = Checker() for row in 0..<3 { for col in 0..<3 { let char = board[blockRow*3 + row][blockCol*3 + col] guard checker.check(char) else { return false } } } } } return true } } Runtime: 135 ms(40%)