Advertisement
Guest User

Untitled

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