Equal Row and Column Pairs

Medium

Description

Given an n x n integer matrix, return the number of pairs (ri, cj) where row ri and column cj are equal as sequences.

Examples

Input:grid = [[3,2,1],[1,7,6],[2,7,7]]
Output:1
Explanation:

Row 2 equals column 1.

Input:grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
Output:3
Explanation:

Row 0: [3,1,2,2], Row 1: [1,4,4,5], Row 2: [2,4,2,2], Row 3: [2,4,2,2]. Column 0: [3,1,2,2], Column 1: [1,4,4,4], Column 2: [2,4,2,2], Column 3: [2,5,2,2]. Row 0 equals Column 0, Row 2 equals Column 2, Row 3 equals Column 2. Total: 3 pairs.

Input:grid = [[1,2],[3,4]]
Output:0
Explanation:

Row 0: [1,2], Row 1: [3,4]. Column 0: [1,3], Column 1: [2,4]. No row equals any column, so there are 0 pairs.

Constraints

  • 1 ≤ n ≤ 200

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!