You are given an m x n binary matrix matrix.
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 0 to 1 or vice versa).
Return the maximum number of rows that have all values equal after some number of flips.
Example 1:
Input: matrix = [[0,1],[1,1]] Output: 1 Explanation: After flipping no values, 1 row has all values equal.
Example 2:
Input: matrix = [[0,1],[1,0]] Output: 2 Explanation: After flipping values in the first column, both rows have equal values.
Example 3:
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:
m == matrix.lengthn == matrix[i].length1 <= m, n <= 300matrix[i][j] is either 0 or 1.Problem Overview: You are given a binary matrix. You can flip any column (change 0 → 1 and 1 → 0 for that column). The goal is to maximize the number of rows that become identical after any number of column flips.
The key observation: two rows can become equal if one row is exactly the same as the other, or if it is the complete inverse of the other. Flipping columns globally affects every row, so rows that share the same relative pattern will align after the same column operations.
Approach 1: Row Pattern Matching using HashMap (Time: O(m*n), Space: O(m*n))
Iterate through each row and convert it into a canonical pattern. If the first element of a row is 1, flip the entire logical representation of the row while building the pattern. This normalizes rows and their inverses into the same representation. Store the normalized pattern as a string or tuple in a hash map and count its frequency. Rows that map to the same pattern can all become identical after flipping the same set of columns. The maximum frequency in the hash map is the answer.
This works because column flips affect every row equally. Normalizing rows relative to the first element groups rows that differ only by a global column flip configuration. Hash lookups provide constant-time aggregation of identical patterns.
Approach 2: Row Pattern Counting with Initial Flip Consideration (Time: O(m*n), Space: O(m*n))
Another way to think about the same idea is to explicitly consider both the original row and its inverted form. For each row, build two patterns: the row itself and the row with all bits flipped. Insert both patterns into a frequency map and count how many rows can match either configuration. Rows that are complements of each other become identical once the correct columns are flipped.
This method is conceptually straightforward and highlights the symmetry between rows and their complements. However, it performs slightly more work per row because both patterns are generated.
The solution relies heavily on pattern grouping using a hash table. The matrix traversal is a simple nested loop over rows and columns using basic array operations, and the data structure being processed is a binary matrix.
Recommended for interviews: The row normalization hash map approach is what most interviewers expect. It demonstrates that you noticed the core invariant: rows and their complements behave the same under column flips. A brute-force simulation of flips would explode combinatorially, while the hash-based pattern grouping keeps the complexity linear in the size of the matrix.
By considering identical or opposite binary patterns within rows, the task reduces to finding the maximum similar pattern. If row 'A' can be made equal to row 'B', they must have the same or opposite pattern for all columns.
Transform each row into a consistent pattern using a reference row and check for identical transformations. The transformation involves flipping columns only when required to match the pattern.
Time Complexity: O(m * m * n) where m is the number of rows and n is the number of columns. Space Complexity: O(n) for the transformation key.
This alternative method evaluates each row by considering both its native and entirely flipped transformations, tallying frequency counts using these dual patterns in order to discover the maximal frequency count of equivalent rows.
This approach introduces a hashing mechanism to efficiently count the occurrences of different row patterns. Two interpretations, original and flipped, are hashed for each row, then comparison pairs are reduced via a single pass lookup.
Time Complexity: O(m * n), Space Complexity: Usage is reduced because of the hash bucket with a distant hash-field.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Approach 1: Row Pattern Matching using HashMap | Time Complexity: O(m * m * n) where m is the number of rows and n is the number of columns. Space Complexity: O(n) for the transformation key. |
| Approach 2: Row Pattern Counting with Initial Flip Consideration | Time Complexity: O(m * n), Space Complexity: Usage is reduced because of the hash bucket with a distant hash-field. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Row Pattern Matching using HashMap | O(m*n) | O(m*n) | Best general solution. Normalizes rows to group identical flip outcomes efficiently. |
| Row Pattern Counting with Initial Flip Consideration | O(m*n) | O(m*n) | Useful when reasoning directly about row complements instead of normalization. |
Flip Columns For Maximum Number of Equal Rows - Leetcode 1072 - Python • NeetCodeIO • 12,429 views views
Watch 9 more video solutions →Practice Flip Columns For Maximum Number of Equal Rows with our built-in code editor and test cases.
Practice on FleetCode