Check if Every Row and Column Contains All Numbers LeetCode solution in Java
1class Solution {2public boolean checkValid(int[][] matrix) {3int n = matrix.length;4int num = (n * (n + 1)) / 2; // SUM of n number 1 to n;56for (int i = 0; i < n; i++) {7HashSet<Integer> hs = new HashSet<Integer>();8HashSet<Integer> hs1 = new HashSet<Integer>();910int m = num;11int k = num;1213for (int j = 0; j < n; j++) {14hs.add(matrix[i][j]);15hs1.add(matrix[j][i]);16m -= matrix[i][j];17k -= matrix[j][i];18}1920if (m != 0 || k != 0 || hs.size() != n || hs.size() != n) {21return false;22}23}24return true;25}26}