Equal Row and Column Pairs LeetCode solution in Java
1class Solution {2public int equalPairs(int[][] grid) {3HashMap<String, Integer> map = new HashMap<>();4int row = grid.length;5int col = grid.length;6for (int i = 0; i < row; i++) {7String res = "";8for (int j = 0; j < col; j++) {9res += "-" + grid[i][j];10}11map.put(res, map.getOrDefault(res, 0) + 1);12}13int cnt = 0;14for (int j = 0; j < col; j++) {15String res = "";16for (int i = 0; i < row; i++) {17res += "-" + grid[i][j];18}19cnt += map.getOrDefault(res, 0);20}21return cnt;22}23}