Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
A row and column pair is considered equal if they contain the same elements in the same order (i.e., an equal array).
Example 1:
Input: grid = [[3,2,1],[1,7,6],[2,7,7]] Output: 1 Explanation: There is 1 equal row and column pair: - (Row 2, Column 1): [2,7,7]
Example 2:
Input: grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]] Output: 3 Explanation: There are 3 equal row and column pairs: - (Row 0, Column 0): [3,1,2,2] - (Row 2, Column 2): [2,4,2,2] - (Row 3, Column 2): [2,4,2,2]
Constraints:
n == grid.length == grid[i].length1 <= n <= 2001 <= grid[i][j] <= 105Problem summary: 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. A row and column pair is considered equal if they contain the same elements in the same order (i.e., an equal array).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[[3,2,1],[1,7,6],[2,7,7]]
[[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
delete-greatest-value-in-each-row)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2352: Equal Row and Column Pairs
class Solution {
public int equalPairs(int[][] grid) {
int n = grid.length;
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
int ok = 1;
for (int k = 0; k < n; ++k) {
if (grid[i][k] != grid[k][j]) {
ok = 0;
break;
}
}
ans += ok;
}
}
return ans;
}
}
// Accepted solution for LeetCode #2352: Equal Row and Column Pairs
func equalPairs(grid [][]int) (ans int) {
for i := range grid {
for j := range grid {
ok := 1
for k := range grid {
if grid[i][k] != grid[k][j] {
ok = 0
break
}
}
ans += ok
}
}
return
}
# Accepted solution for LeetCode #2352: Equal Row and Column Pairs
class Solution:
def equalPairs(self, grid: List[List[int]]) -> int:
n = len(grid)
ans = 0
for i in range(n):
for j in range(n):
ans += all(grid[i][k] == grid[k][j] for k in range(n))
return ans
// Accepted solution for LeetCode #2352: Equal Row and Column Pairs
/**
* [2352] Equal Row and Column Pairs
*
* 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.
* A row and column pair is considered equal if they contain the same elements in the same order (i.e., an equal array).
*
* Example 1:
* <img alt="" src="https://assets.leetcode.com/uploads/2022/06/01/ex1.jpg" style="width: 150px; height: 153px;" />
* Input: grid = [[3,2,1],[1,7,6],[2,7,7]]
* Output: 1
* Explanation: There is 1 equal row and column pair:
* - (Row 2, Column 1): [2,7,7]
*
* Example 2:
* <img alt="" src="https://assets.leetcode.com/uploads/2022/06/01/ex2.jpg" style="width: 200px; height: 209px;" />
* Input: grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
* Output: 3
* Explanation: There are 3 equal row and column pairs:
* - (Row 0, Column 0): [3,1,2,2]
* - (Row 2, Column 2): [2,4,2,2]
* - (Row 3, Column 2): [2,4,2,2]
*
*
* Constraints:
*
* n == grid.length == grid[i].length
* 1 <= n <= 200
* 1 <= grid[i][j] <= 10^5
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/equal-row-and-column-pairs/
// discuss: https://leetcode.com/problems/equal-row-and-column-pairs/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn equal_pairs(grid: Vec<Vec<i32>>) -> i32 {
grid.iter().fold(0, |res, row| {
res + grid.iter().enumerate().fold(0, |res_for_row, (ind, _)| {
let column = grid.iter().map(|row| row[ind]).collect::<Vec<i32>>();
res_for_row + (row == &column) as i32
})
})
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2352_example_1() {
let grid = vec![vec![3, 2, 1], vec![1, 7, 6], vec![2, 7, 7]];
let result = 1;
assert_eq!(Solution::equal_pairs(grid), result);
}
#[test]
fn test_2352_example_2() {
let grid = vec![
vec![3, 1, 2, 2],
vec![1, 4, 4, 5],
vec![2, 4, 2, 2],
vec![2, 4, 2, 2],
];
let result = 3;
assert_eq!(Solution::equal_pairs(grid), result);
}
}
// Accepted solution for LeetCode #2352: Equal Row and Column Pairs
function equalPairs(grid: number[][]): number {
const n = grid.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
for (let j = 0; j < n; ++j) {
let ok = 1;
for (let k = 0; k < n; ++k) {
if (grid[i][k] !== grid[k][j]) {
ok = 0;
break;
}
}
ans += ok;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.