2025-08-22 17:12:59 +0000 UTC

Make a Square with the Same Color

Code

class Solution:
    def canMakeSquare(self, grid: List[List[str]]) -> bool:
        rows, cols = len(grid), len(grid[0])
        moves = ((0, 0), (0, -1), (-1, -1), (-1, 0))
        for row in range(1, rows):
            for col in range(1, cols):
                cnt = 0
                for row_dlt, col_dlt in moves:
                    if grid[row + row_dlt][col + col_dlt] == "W":
                        cnt += 1
                    else:
                        cnt -= 1
                if cnt in (2, -2, 4, -4):
                    return True
        return False