Skip to content

Commit e81c169

Browse files
committed
Add solution #2352
1 parent f19e168 commit e81c169

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@
390390
2215|[Find the Difference of Two Arrays](./2215-find-the-difference-of-two-arrays.js)|Easy|
391391
2235|[Add Two Integers](./2235-add-two-integers.js)|Easy|
392392
2244|[Minimum Rounds to Complete All Tasks](./2244-minimum-rounds-to-complete-all-tasks.js)|Medium|
393+
2352|[Equal Row and Column Pairs](./2352-equal-row-and-column-pairs.js)|Medium|
393394
2390|[Removing Stars From a String](./2390-removing-stars-from-a-string.js)|Medium|
394395
2396|[Strictly Palindromic Number](./2396-strictly-palindromic-number.js)|Medium|
395396
2413|[Smallest Even Multiple](./2413-smallest-even-multiple.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 2352. Equal Row and Column Pairs
3+
* https://leetcode.com/problems/equal-row-and-column-pairs/
4+
* Difficulty: Medium
5+
*
6+
* Given a 0-indexed n x n integer matrix grid, return the number of pairs (ri, cj)
7+
* such that row ri and column cj are equal.
8+
*
9+
* A row and column pair is considered equal if they contain the same elements in the
10+
* same order (i.e., an equal array).
11+
*/
12+
13+
/**
14+
* @param {number[][]} grid
15+
* @return {number}
16+
*/
17+
var equalPairs = function(grid) {
18+
const columns = new Map();
19+
let count = 0;
20+
21+
for (let i = 0; i < grid.length; i++) {
22+
const column = [];
23+
for (let j = 0; j < grid[i].length; j++) {
24+
column.push(grid[j][i]);
25+
}
26+
columns.set(column.join(), (columns.get(column.join()) ?? 0) + 1);
27+
}
28+
29+
for (let i = 0; i < grid.length; i++) {
30+
count += columns.get(grid[i].join()) ?? 0;
31+
}
32+
33+
return count;
34+
};

0 commit comments

Comments
 (0)