Remove All Ones With Row and Column Flips Problem
Description
LeetCode Problem 2128.
You are given an m x n binary matrix grid.
In one operation, you can choose any row or column and flip each value in that row or column (i.e., changing all 0's to 1's, and all 1's to 0's).
Return true if it is possible to remove all 1's from grid using any number of operations or false otherwise.
Example 1:

Input: grid = [[0,1,0],[1,0,1],[0,1,0]]
Output: true
Explanation: One possible way to remove all 1's from grid is to:
- Flip the middle row
- Flip the middle column
Example 2:

Input: grid = [[1,1,0],[0,0,0],[0,0,0]]
Output: false
Explanation: It is impossible to remove all 1's from grid.
Example 3:

Input: grid = [[0]]
Output: true
Explanation: There are no 1's in grid.
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 300grid[i][j]is either0or1.
Solution
Approach: Normalizing Rows
Flipping rows and columns keeps the pattern of equal and different cells between any two rows: each row must end up either equal to the first row or its complement. So the grid can be cleared exactly when every row equals the first row or is its exact opposite. Flip every row that starts with 1 (so each row starts with 0) and check that all rows are then identical, using a set.
Complexity: time O(mn); space O(m).
class Solution:
def removeOnes(self, grid: List[List[int]]) -> bool:
s = set()
for row in grid:
t = tuple(row) if row[0] == grid[0][0] else tuple(x ^ 1 for x in row)
s.add(t)
return len(s) == 1