Getting everything ready for you…

Loading courses, problems and your progress. This takes just a moment.

0s

Practice › Hashing & Two Pointers

Valid Sudoku

Medium · Hashing & Two Pointers · Asked at Amazon, Apple, Uber, Microsoft

A partly filled 9 × 9 Sudoku board is valid if no digit repeats in any row, any column or any of the nine 3 × 3 boxes. Empty cells are written as .. Print YES if the board is valid, otherwise NO (it does not have to be solvable).

Input

9 lines of 9 characters each (1–9 or .).

Output

YES or NO.

Constraints

Keep a seen-set for every row, every column and every box (box index = (row / 3) × 3 + column / 3). A digit seen twice in any of them makes the board invalid.

Example 1

Input

53..7....
6..195...
.98....6.
8...6...3
4..8.3..1
7...2...6
.6....28.
...419..5
....8..79

Output

YES

Example 2

Input

83..7....
6..195...
.98....6.
8...6...3
4..8.3..1
7...2...6
.6....28.
...419..5
....8..79

Output

NO

Hints

  1. Three kinds of groups: 9 rows, 9 columns, 9 boxes.
  2. The box of cell (r, c) is (r / 3) × 3 + c / 3.
  3. Track which digits each group has seen.

Approach

Keep boolean tables row[9][10], col[9][10] and box[9][10]. For every filled cell with digit d, if row[r][d], col[c][d] or box[b][d] is already set, print NO; otherwise set all three. If the scan finishes, print YES. Empty cells are skipped — the board does not need to be solvable.

Complexity: O(81) time · O(1) space

Solution

C++ solution

#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<string> g(9);
    for (auto &row : g) cin >> row;
    bool row[9][10] = {{false}}, col[9][10] = {{false}}, box[9][10] = {{false}};
    for (int i = 0; i < 9; i++)
        for (int j = 0; j < 9; j++) {
            if (g[i][j] == '.') continue;
            int d = g[i][j] - '0', b = (i / 3) * 3 + j / 3;
            if (row[i][d] || col[j][d] || box[b][d]) {
                cout << "NO\n";
                return 0;
            }
            row[i][d] = col[j][d] = box[b][d] = true;
        }
    cout << "YES\n";
    return 0;
}

Python solution

import sys

g = sys.stdin.read().split()[:9]
seen = set()
ok = True
for i in range(9):
    for j in range(9):
        c = g[i][j]
        if c == ".":
            continue
        keys = (("r", i, c), ("c", j, c), ("b", i // 3, j // 3, c))
        if any(k in seen for k in keys):
            ok = False
        seen.update(keys)
print("YES" if ok else "NO")

Java solution

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] g = new String[9];
        for (int i = 0; i < 9; i++) g[i] = br.readLine().trim();
        boolean[][] row = new boolean[9][10], col = new boolean[9][10], box = new boolean[9][10];
        for (int i = 0; i < 9; i++)
            for (int j = 0; j < 9; j++) {
                char c = g[i].charAt(j);
                if (c == '.') continue;
                int d = c - '0', b = (i / 3) * 3 + j / 3;
                if (row[i][d] || col[j][d] || box[b][d]) {
                    System.out.println("NO");
                    return;
                }
                row[i][d] = col[j][d] = box[b][d] = true;
            }
        System.out.println("YES");
    }
}

Solve Valid Sudoku online · All practice problems