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<>();
- int count = 0;
- //rows
- for (int i = 0; i < 9; i++)
- {
- for (int j = 0; j < 9; j++)
- {
- if (board[i][j] != '.')
- {
- myHashSet.add(board[i][j]);
- count++;
- }
- }
- if(myHashSet.size()!=count)
- return false;
- myHashSet.clear();
- count = 0;
- }
- //columns
- for (int i = 0; i < 9; i++)
- {
- for (int j = 0; j < 9; j++)
- {
- if (board[j][i] != '.')
- {
- myHashSet.add(board[j][i]);
- count++;
- }
- }
- if(myHashSet.size()!=count)
- return false;
- myHashSet.clear();
- count = 0;
- }
- //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[j][i] != '.')
- {
- myHashSet.add(board[j][i]);
- count++;
- }
- }
- }
- if(myHashSet.size()!=count)
- return false;
- myHashSet.clear();
- count = 0;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement