Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public boolean isValidSudoku(char[][] board) {
- HashSet<Character> myHashSet = new HashSet<>();
- //rows
- for (int i = 0; i < 9; i++) {
- for (int j = 0; j < 9; j++) {
- if (board[i][j] != '.') {
- if (!myHashSet.contains(board[i][j])) {
- myHashSet.add(board[i][j]);
- } else {
- return false;
- }
- }
- }
- myHashSet.clear();
- }
- //columns
- for (int i = 0; i < 9; i++) {
- for (int j = 0; j < 9; j++) {
- if (board[j][i] != '.') {
- if (!myHashSet.contains(board[j][i])) {
- myHashSet.add(board[j][i]);
- } else {
- return false;
- }
- }
- }
- myHashSet.clear();
- }
- //3x3
- for (int i = 0; i < 9; i += 3) {
- for (int j = 0; j < 9; j += 3) {
- for (int m = i; m < i+2; m++) {
- for (int n = j; n < j+2; n++) {
- if (board[m][n] != '.') {
- if (!myHashSet.contains(board[m][n])) {
- myHashSet.add(board[m][n]);
- } else {
- return false;
- }
- }
- }
- myHashSet.clear();
- }
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement