|
| 1 | +# 2352. Equal Row and Column Pairs |
| 2 | + |
| 3 | +**Difficulty:** Medium |
| 4 | +**Source:** [Weekly Contest 303 - Q2](https://leetcode.com/contest/weekly-contest-303) |
| 5 | +**Tags:** Array, Hash Table, Matrix, Simulation |
| 6 | +**Rating:** 1286 |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## Problem |
| 11 | + |
| 12 | +Given a **0-indexed** `n x n` integer matrix `grid`, return the number of pairs `(ri, cj)` such that row `ri` and column `cj` are equal. |
| 13 | + |
| 14 | +A row and column pair is considered equal if they contain the same elements in the same order (i.e., an equal array). |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Examples |
| 19 | + |
| 20 | +### Example 1: |
| 21 | + |
| 22 | +**Input:** |
| 23 | + |
| 24 | +``` |
| 25 | +grid = [[3,2,1], |
| 26 | + [1,7,6], |
| 27 | + [2,7,7]] |
| 28 | +``` |
| 29 | + |
| 30 | +**Output:** `1` |
| 31 | +**Explanation:** There is 1 equal row and column pair: |
| 32 | +- (Row 2, Column 1): [2,7,7] |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +### Example 2: |
| 37 | + |
| 38 | +**Input:** |
| 39 | + |
| 40 | +``` |
| 41 | +grid = [[3,1,2,2], |
| 42 | + [1,4,4,5], |
| 43 | + [2,4,2,2], |
| 44 | + [2,4,2,2]] |
| 45 | +``` |
| 46 | + |
| 47 | +**Output:** `3` |
| 48 | +**Explanation:** There are 3 equal row and column pairs: |
| 49 | +- (Row 0, Column 0): [3,1,2,2] |
| 50 | +- (Row 2, Column 2): [2,4,2,2] |
| 51 | +- (Row 3, Column 2): [2,4,2,2] |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +## Constraints |
| 56 | + |
| 57 | +- `n == grid.length == grid[i].length` |
| 58 | +- `1 <= n <= 200` |
| 59 | +- `1 <= grid[i][j] <= 10⁵` |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## Explanation |
| 64 | + |
| 65 | +We need to count the number of times a row in the grid exactly matches a column. One efficient way to approach this is: |
| 66 | + |
| 67 | +1. Store all row vectors in a hash map with their frequencies. |
| 68 | +2. For each column vector, check if it exists in the row hash map and count how many times. |
| 69 | + |
| 70 | +This reduces redundant comparisons and avoids brute force $O(n^3)$ complexity. |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +## Solutions |
| 75 | + |
| 76 | +### Python3 |
| 77 | + |
| 78 | +```python |
| 79 | +from collections import Counter |
| 80 | + |
| 81 | +class Solution: |
| 82 | + def equalPairs(self, grid: List[List[int]]) -> int: |
| 83 | + n = len(grid) |
| 84 | + row_map = Counter(tuple(row) for row in grid) |
| 85 | + count = 0 |
| 86 | + for col in zip(*grid): |
| 87 | + count += row_map[tuple(col)] |
| 88 | + return count |
| 89 | +``` |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +### Java |
| 94 | + |
| 95 | +```java |
| 96 | +class Solution { |
| 97 | + public int equalPairs(int[][] grid) { |
| 98 | + int n = grid.length; |
| 99 | + Map<String, Integer> rowMap = new HashMap<>(); |
| 100 | + for (int[] row : grid) { |
| 101 | + String key = Arrays.toString(row); |
| 102 | + rowMap.put(key, rowMap.getOrDefault(key, 0) + 1); |
| 103 | + } |
| 104 | + |
| 105 | + int count = 0; |
| 106 | + for (int j = 0; j < n; j++) { |
| 107 | + int[] col = new int[n]; |
| 108 | + for (int i = 0; i < n; i++) { |
| 109 | + col[i] = grid[i][j]; |
| 110 | + } |
| 111 | + String key = Arrays.toString(col); |
| 112 | + count += rowMap.getOrDefault(key, 0); |
| 113 | + } |
| 114 | + |
| 115 | + return count; |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +### C++ |
| 123 | + |
| 124 | +```cpp |
| 125 | +class Solution { |
| 126 | +public: |
| 127 | + int equalPairs(vector<vector<int>>& grid) { |
| 128 | + int n = grid.size(); |
| 129 | + map<vector<int>, int> rowMap; |
| 130 | + for (auto& row : grid) { |
| 131 | + rowMap[row]++; |
| 132 | + } |
| 133 | + |
| 134 | + int count = 0; |
| 135 | + for (int j = 0; j < n; ++j) { |
| 136 | + vector<int> col; |
| 137 | + for (int i = 0; i < n; ++i) { |
| 138 | + col.push_back(grid[i][j]); |
| 139 | + } |
| 140 | + count += rowMap[col]; |
| 141 | + } |
| 142 | + |
| 143 | + return count; |
| 144 | + } |
| 145 | +}; |
| 146 | +``` |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +### Go |
| 151 | + |
| 152 | +```go |
| 153 | +func equalPairs(grid [][]int) int { |
| 154 | + n := len(grid) |
| 155 | + rowMap := map[string]int{} |
| 156 | + for _, row := range grid { |
| 157 | + key := fmt.Sprint(row) |
| 158 | + rowMap[key]++ |
| 159 | + } |
| 160 | + |
| 161 | + count := 0 |
| 162 | + for j := 0; j < n; j++ { |
| 163 | + col := []int{} |
| 164 | + for i := 0; i < n; i++ { |
| 165 | + col = append(col, grid[i][j]) |
| 166 | + } |
| 167 | + key := fmt.Sprint(col) |
| 168 | + count += rowMap[key] |
| 169 | + } |
| 170 | + return count |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +--- |
| 175 | + |
| 176 | +### TypeScript |
| 177 | + |
| 178 | +```ts |
| 179 | +function equalPairs(grid: number[][]): number { |
| 180 | + const n = grid.length; |
| 181 | + const rowMap = new Map<string, number>(); |
| 182 | + |
| 183 | + for (const row of grid) { |
| 184 | + const key = row.join(','); |
| 185 | + rowMap.set(key, (rowMap.get(key) ?? 0) + 1); |
| 186 | + } |
| 187 | + |
| 188 | + let count = 0; |
| 189 | + for (let j = 0; j < n; ++j) { |
| 190 | + const col: number[] = []; |
| 191 | + for (let i = 0; i < n; ++i) { |
| 192 | + col.push(grid[i][j]); |
| 193 | + } |
| 194 | + const key = col.join(','); |
| 195 | + count += rowMap.get(key) ?? 0; |
| 196 | + } |
| 197 | + |
| 198 | + return count; |
| 199 | +} |
| 200 | +``` |
| 201 | + |
0 commit comments