Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. class Solution {
  2. public boolean isValidSudoku(char[][] board) {
  3. HashSet<Character> myHashSet = new HashSet<>();
  4. //rows
  5. for (int i = 0; i < 9; i++) {
  6. for (int j = 0; j < 9; j++) {
  7. if (board[i][j] != '.') {
  8. if (!myHashSet.contains(board[i][j])) {
  9. myHashSet.add(board[i][j]);
  10. } else {
  11. return false;
  12. }
  13. }
  14. }
  15. myHashSet.clear();
  16. }
  17. //columns
  18. for (int i = 0; i < 9; i++) {
  19. for (int j = 0; j < 9; j++) {
  20. if (board[j][i] != '.') {
  21. if (!myHashSet.contains(board[j][i])) {
  22. myHashSet.add(board[j][i]);
  23. } else {
  24. return false;
  25. }
  26. }
  27. }
  28. myHashSet.clear();
  29. }
  30. //3x3
  31. for (int i = 0; i < 9; i += 3) {
  32. for (int j = 0; j < 9; j += 3) {
  33. for (int m = i; m < i+2; m++) {
  34. for (int n = j; n < j+2; n++) {
  35. if (board[m][n] != '.') {
  36. if (!myHashSet.contains(board[m][n])) {
  37. myHashSet.add(board[m][n]);
  38. } else {
  39. return false;
  40. }
  41. }
  42. }
  43. myHashSet.clear();
  44. }
  45. }
  46. }
  47. return true;
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement