1147. Flip Columns For Maximum Number of Equal Rows
My accepted Python solution to LeetCode problem 1147, Flip Columns For Maximum Number of Equal Rows, running in 38ms.
- Difficulty: Medium
- Python
- Runtime 38ms
- Memory 20.3MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 38ms, memory 20.3MB, accepted 2025-12-29.
class Solution:
def maxEqualRowsAfterFlips(self, matrix: List[List[int]]) -> int:
# Two rows can be made equal if one is the complement of the other or same
# Use pattern matching - normalize each row to start with 0
# Time: O(m*n), Space: O(m*n)
from collections import Counter
patterns = Counter()
for row in matrix:
# Normalize: if row[0] is 1, flip all bits
if row[0] == 1:
pattern = tuple(1 - x for x in row)
else:
pattern = tuple(row)
patterns[pattern] += 1
return max(patterns.values())