Daily LeetCode Problems: Problem 2352. Equal Row and Column Pairs

Solving LeetCode Problem 2352: Equal Row and Column Pairs

Monit Sharma
3 min readJun 13, 2023
Total Problems solved : 432

Welcome to another daily article on LeetCode problems! In today’s article, we will discuss problem 2352, “Equal Row and Column Pairs”. We will carefully examine the problem statement, discuss an approach to solving it, provide pseudocode for the solution, and discuss some implementation details. Let’s get started!

Problem Statement

The problem requires us to count the number of pairs (ri, cj) such that row ri and column cj are equal in a given matrix grid. A row and column pair is considered equal if they contain the same elements in the same order.

Example

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]

Approach

To solve this problem, we will iterate through each row and column in the matrix and compare them. We will perform the following steps:

  1. Initialize a variable count to keep track of the count of equal row and column pairs. Set it to 0.
  2. For each row and column in the matrix:
  • Check if the row and column are equal by comparing their elements.
  • If they are equal, increment count by 1.

3. Finally, count will contain the total count of equal row and column pairs.

Pseudocode

Let’s express the solution steps in pseudocode:

function countEqualPairs(grid):
Initialize count as 0.

For each row in grid:
For each column in grid:
Check if row and column are equal by comparing their elements.
If they are equal, increment count by 1.

Return count as the count of equal row and column pairs.

Implementation Details

Implementing the countEqualPairs function in your preferred programming language should be straightforward. You can use nested loops to iterate through the rows and columns of the matrix. Remember to handle edge cases, such as when the matrix is empty or when it has different numbers of rows or columns.

Full Solution

Python

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):
k = 0
while k < n:
if grid[i][k] != grid[k][j]:
break
k += 1
if k == n: # R[i] == C[j]
ans += 1

return ans

Java

class Solution {
public int equalPairs(int[][] grid) {
final int n = grid.length;
int ans = 0;

for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) {
int k = 0;
for (; k < n; ++k)
if (grid[i][k] != grid[k][j])
break;
if (k == n) // R[i] == C[j]
++ans;
}

return ans;
}
}

C++

class Solution {
public:
int equalPairs(vector<vector<int>>& grid) {
const int n = grid.size();
int ans = 0;

for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) {
int k = 0;
for (; k < n; ++k)
if (grid[i][k] != grid[k][j])
break;
if (k == n) // R[i] == C[j]
++ans;
}

return ans;
}
};

Conclusion

In this article, we explored LeetCode problem 2352, “Equal Row and Column Pairs”. We carefully examined the problem statement, discussed an approach to count the number of pairs where a row and column are equal, provided pseudocode for the solution, and discussed some implementation details.

Remember, consistent practice and problem-solving are key to improving your coding skills. So, make it a habit to challenge yourself with more LeetCode problems regularly. Happy coding!

--

--