LeetCode/LeetCode75

[LeetCode 75] Medium - 2352. Equal Row and Column Pairs

hyunkookim 2024. 11. 14. 15:12

2352. Equal Row and Column Pairs

 

해시맵 으로..

 

class Solution:
    def equalPairs(self, grid: List[List[int]]) -> int:
        """
        grid [ [c1 c2 c3]    r1
               [c1 c2 c3]    r2
               [c1 c2 c3] ]  r3
        """
        n = len(grid)
        row = {}
        for i in range(n):
            if tuple(grid[i]) in row:
                row[tuple(grid[i])] +=1
            else:
                row[tuple(grid[i])] =1

        res = 0
        for c in range(n):
            col = [grid[r][c] for r in range(n)]

            if tuple(col) in row:
                res += row[tuple(col)]
        return res

 

https://youtu.be/6cifzEaEEX8?si=pDgqZG5is6n8xNxY