Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #782: Transform to Chessboard

In this guide, we solve Leetcode #782 Transform to Chessboard in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

You are given an n x n binary grid board. In each move, you can swap any two rows with each other, or any two columns with each other.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Bit Manipulation, Array, Math, Matrix

Intuition

The problem structure lets us track state with bitwise operations.

Bit operations are constant time and avoid extra memory.

Approach

Apply XOR/AND/OR and shifts to maintain the required invariant.

Aggregate the result in a single pass.

Steps:

  • Identify a bitwise invariant.
  • Combine values with bit operations.
  • Return the aggregated result.

Example

Input: board = [[0,1,1,0],[0,1,1,0],[1,0,0,1],[1,0,0,1]] Output: 2 Explanation: One potential sequence of moves is shown. The first move swaps the first and second column. The second move swaps the second and third row.

Python Solution

class Solution: def movesToChessboard(self, board: List[List[int]]) -> int: def f(mask, cnt): ones = mask.bit_count() if n & 1: if abs(n - 2 * ones) != 1 or abs(n - 2 * cnt) != 1: return -1 if ones == n // 2: return n // 2 - (mask & 0xAAAAAAAA).bit_count() return (n + 1) // 2 - (mask & 0x55555555).bit_count() else: if ones != n // 2 or cnt != n // 2: return -1 cnt0 = n // 2 - (mask & 0xAAAAAAAA).bit_count() cnt1 = n // 2 - (mask & 0x55555555).bit_count() return min(cnt0, cnt1) n = len(board) mask = (1 << n) - 1 rowMask = colMask = 0 for i in range(n): rowMask |= board[0][i] << i colMask |= board[i][0] << i revRowMask = mask ^ rowMask revColMask = mask ^ colMask sameRow = sameCol = 0 for i in range(n): curRowMask = curColMask = 0 for j in range(n): curRowMask |= board[i][j] << j curColMask |= board[j][i] << j if curRowMask not in (rowMask, revRowMask) or curColMask not in ( colMask, revColMask, ): return -1 sameRow += curRowMask == rowMask sameCol += curColMask == colMask t1 = f(rowMask, sameRow) t2 = f(colMask, sameCol) return -1 if t1 == -1 or t2 == -1 else t1 + t2

Complexity

The time complexity is O(n2)O(n^2)O(n2), where nnn is the size of the chessboard. The space complexity is O(1)O(1)O(1).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy