diff --git a/README.md b/README.md index a46c905..7a97b51 100644 --- a/README.md +++ b/README.md @@ -132,11 +132,12 @@ Ace Coding Interview with 75 Qs | 2215. Find the Difference of Two Arrays | [Solution][2215] | Easy | | 1207. Unique Number of Occurrences | [Solution][1207] | Easy | | 1657. Determine if Two Strings Are Close | [Solution][1657] | Medium | -| 2352. Equal Row and Column Pairs | Solution | Medium | +| 2352. Equal Row and Column Pairs | [Solution][2352] | Medium | [2215]: ./src/page-21/2215.%20Find%20the%20Difference%20of%20Two%20Arrays/findDifference.ts [1207]: ./src/page-12/1207.%20Unique%20Number%20of%20Occurrences/uniqueOccurrences.ts [1657]: ./src/page-16/1657.%20Determine%20if%20Two%20Strings%20Are%20Close/closeStrings.ts +[2352]: ./src/page-22/2352.%20Equal%20Row%20and%20Column%20Pairs/equalPairs.ts | Stack | | | | ---------------------------------- | -------- | ------ | diff --git a/src/page-22/2352. Equal Row and Column Pairs/equalPairs.test.ts b/src/page-22/2352. Equal Row and Column Pairs/equalPairs.test.ts new file mode 100644 index 0000000..16c6f33 --- /dev/null +++ b/src/page-22/2352. Equal Row and Column Pairs/equalPairs.test.ts @@ -0,0 +1,22 @@ +import { equalPairs } from './equalPairs'; + +describe('2352. Equal Row and Column Pairs', () => { + test('equalPairs', () => { + expect( + equalPairs([ + [3, 2, 1], + [1, 7, 6], + [2, 7, 7], + ]), + ).toBe(1); + + expect( + equalPairs([ + [3, 1, 2, 2], + [1, 4, 4, 5], + [2, 4, 2, 2], + [2, 4, 2, 2], + ]), + ).toBe(3); + }); +}); diff --git a/src/page-22/2352. Equal Row and Column Pairs/equalPairs.ts b/src/page-22/2352. Equal Row and Column Pairs/equalPairs.ts new file mode 100644 index 0000000..044b9e8 --- /dev/null +++ b/src/page-22/2352. Equal Row and Column Pairs/equalPairs.ts @@ -0,0 +1,24 @@ +type EqualPairs = (grid: number[][]) => number; + +/** + * Accepted + */ +export const equalPairs: EqualPairs = (grid) => { + const rowMap = new Map(); + + // Store each row in the map + for (let i = 0; i < grid.length; i++) { + const row = grid[i].join(); + rowMap.set(row, (rowMap.get(row) || 0) + 1); + } + + let count = 0; + + // Check each column against the stored rows + for (let j = 0; j < grid.length; j++) { + const col = grid.map((row) => row[j]).join(); + count += rowMap.get(col) || 0; + } + + return count; +};