2023-06-14 23:43:34 +02:00
|
|
|
class Solution {
|
2024-03-02 21:01:53 +01:00
|
|
|
public int equalPairs(int[][] grid) {
|
|
|
|
int count = 0, n = grid.length;
|
2023-06-14 23:43:34 +02:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
// Check each row r against each column c.
|
|
|
|
for (int r = 0; r < n; ++r) {
|
|
|
|
for (int c = 0; c < n; ++c) {
|
|
|
|
boolean match = true;
|
2023-06-14 23:43:34 +02:00
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
// Iterate over row r and column c.
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
if (grid[r][i] != grid[i][c]) {
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
2023-06-14 23:43:34 +02:00
|
|
|
}
|
|
|
|
|
2024-03-02 21:01:53 +01:00
|
|
|
// If row r equals column c, increment count by 1.
|
|
|
|
if (match) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 23:43:34 +02:00
|
|
|
}
|
2024-03-02 21:01:53 +01:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
2023-06-14 23:43:34 +02:00
|
|
|
}
|