> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/06-01-2022-36.-valid-sudoku.md).

# 06/01/2022 36. Valid Sudoku

Method: use hashset for each rows, cols and boxes to indicate if there is any duplicate characters. The most diffcult part in this quesiton is get the 3\*3 box numbers. Here we can use **i/3 \* 3 + j/3** to get contiguous number from 0-8 to diffentiate each box. Here 3 is the columns number in each small box.&#x20;

Time O(n^2)

Space O(n^2)

```
// Some code
class Solution {
    public boolean isValidSudoku(char[][] board) {
        Set<Character> [] row = new HashSet[9];
        Set<Character> [] col = new HashSet[9];
        Set<Character> [] box = new HashSet[9];
        
        for (int i = 0; i < 9; i++){
            row[i] = new HashSet<>();
            col[i] = new HashSet<>();
            box[i] = new HashSet<>();
        }
        
        for (int i = 0; i < 9; i++){
            for (int j = 0; j < 9; j++){
                char ch = board[i][j];
                if (ch == '.')
                    continue;
                if (row[i].contains(ch)) return false;
                row[i].add(ch);
                
                if (col[j].contains(ch)) return false;
                col[j].add(ch);
                
                if (box[(i/3) * 3 + j/3].contains(ch)) return false;
                box[(i/3) * 3 + j/3].add(ch);
                
            }
        }
        
        return true;
        
    }
}
```
