> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Flip Columns For Maximum Number of Equal Rows

> Tested Python solution for LeetCode 1072 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 1072, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/flip-columns-for-maximum-number-of-equal-rows/description/).

Generate this problem as a practice environment: tested reference solution, 13 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1072   # by problem number
lcpy gen -s flip_columns_for_maximum_number_of_equal_rows   # by problem name
```

## Problem

You are given an \<code>m x n\</code> binary matrix \<code>matrix\</code>.

You can choose any number of columns in the matrix and flip every cell in that column (i.e., Change the value of the cell from \<code>0\</code> to \<code>1\</code> or vice versa).

Return \<em>the maximum number of rows that have all values equal after some number of flips\</em>.

### Examples

```
Input: matrix = [[0,1],[1,1]]
Output: 1
Explanation: After flipping no values, 1 row has all values equal.
```

```
Input: matrix = [[0,1],[1,0]]
Output: 2
Explanation: After flipping values in the first column, both rows have equal values.
```

```
Input: matrix = [[0,0,0],[0,0,1],[1,1,0]]
Output: 2
Explanation: After flipping values in the first two columns, the last two rows have equal values.
```

### Constraints

* \<code>m == matrix.length\</code>
* \<code>n == matrix\[i].length\</code>
* \<code>1 \<= m, n \<= 300\</code>
* \<code>matrix\[i]\[j]\</code> is either \<code>0\</code> or \<code>1\</code>.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/flip_columns_for_maximum_number_of_equal_rows/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/flip_columns_for_maximum_number_of_equal_rows/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m * n)
    # Space: O(m * n)
    def max_equal_rows_after_flips(self, matrix: list[list[int]]) -> int:
        counts: dict[tuple[int, ...], int] = {}
        for row in matrix:
            key = tuple(row) if row[0] == 0 else tuple(1 - x for x in row)
            counts[key] = counts.get(key, 0) + 1
        return max(counts.values())
```

## Complexity

| Time | Space |
| - | - |
| O(m \* n) | O(m \* n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.