Skip to content

Commit f6a1f01

Browse files
committed
Add: Exercise 2352. Equal Row and Column Pairs
1 parent b6bc426 commit f6a1f01

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 2352. Equal Row and Column Pairs
2+
Given a 0-indexed `n x n` integer matrix `grid`, return the number of pairs `(ri, cj)` such that row
3+
`ri` and column `cj` are equal.
4+
5+
A row and column pair is considered equal if they contain the same elements in the same order (i.e.,
6+
an equal array).
7+
8+
## Examples
9+
10+
### Example 1:
11+
12+
Input: `grid` = [[3,2,1],[1,7,6],[2,7,7]]
13+
Output: 1
14+
Explanation: There is 1 equal row and column pair:
15+
- (Row 2, Column 1): [2,7,7]
16+
17+
### Example 2:
18+
19+
Input: `grid` = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
20+
Output: 3
21+
Explanation: There are 3 equal row and column pairs:
22+
- (Row 0, Column 0): [3,1,2,2]
23+
- (Row 2, Column 2): [2,4,2,2]
24+
- (Row 3, Column 2): [2,4,2,2]
25+
26+
## Constraints
27+
28+
- `n == grid.length == grid[i].length`
29+
- `1 <= n <= 200`
30+
- `1 <= grid[i][j] <= 10^5`
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Counts the number of equal row-column pairs in a square matrix.
3+
* @param {number[][]} grid - The input square matrix.
4+
* @return {number} - The number of equal row-column pairs.
5+
*/
6+
var equalPairs = function(grid) {
7+
// Check that it's an square matrix
8+
if (grid.length !== grid[0].length) return 0
9+
10+
// Initialize variables
11+
const rows = {}
12+
const columns = {}
13+
let equalPairsCount = 0
14+
15+
// Populate the `rows` object, convert row contents to string for further comparison
16+
for (let i = 0; i < grid.length; i++) {
17+
rows[i] = grid[i].join()
18+
}
19+
20+
// Populate the `columns` object, convert columns to string
21+
for (let i = 0; i < grid.length; i++) {
22+
for (let j = 0; j < grid.length; j++) {
23+
// We'll use commas to help us distinguish between columns and rows with similar
24+
// characters. For example if row = [11,1] and column = [1,11] are compare as strings,
25+
// without commas, the comparison will be '111' === '111' which will cause a false
26+
// positive
27+
columns[j] ?
28+
columns[j] = columns[j] + ',' + grid[i][j].toString() :
29+
columns[j] = '' + grid[i][j].toString()
30+
}
31+
}
32+
33+
// Compare rows with columns and increment the pair counter
34+
for (const row in rows) {
35+
for(const column in columns) {
36+
if (rows[row] === columns[column]) {
37+
equalPairsCount++
38+
}
39+
}
40+
}
41+
42+
return equalPairsCount
43+
};
44+
45+
const grid = [[3,2,1],[1,7,6],[2,7,7]]
46+
var output = equalPairs(grid)
47+
48+
module.exports = equalPairs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "2352-equal-row-and-column-pairs",
3+
"version": "1.0.0",
4+
"description": "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.",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"debug": "node --inspect-brk index.js",
9+
"test": "node test.js"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC"
14+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const assert = require('assert')
2+
const equalPairs = require('./index')
3+
4+
const tests = [
5+
{
6+
grid: [[3,2,1],[1,7,6],[2,7,7]],
7+
expectedOutput: 1
8+
},
9+
{
10+
grid: [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]],
11+
expectedOutput: 3,
12+
},
13+
{
14+
grid: [[11,1],[1,11]],
15+
expectedOutput: 2,
16+
},
17+
]
18+
19+
for (const t of tests) {
20+
assert.strict.strictEqual(equalPairs(t.grid), t.expectedOutput)
21+
}

0 commit comments

Comments
 (0)