File: determine-whether-matrix-can-be-obtained-by-rotation/solution.py

Date: 2026-06-06

Time: 16:21

determine-whether-matrix-can-be-obtained-by-rotation/solution.py

Purpose

This file solves LeetCode 1886: Determine Whether Matrix Can Be Obtained By Rotation. It contains both the solution and its test suite in a single module — the standard layout for this repo. The file's sole responsibility is determining whether an n×n matrix can become a target matrix through 0, 90, 180, or 270-degree clockwise rotations.

Key Components

Solution.findRotation(mat, target) -> bool — The only public method. Takes two square matrices and returns whether any rotation of mat equals target. The method is destructive to its local binding of mat (reassigns it on each rotation) but does not mutate the caller's list, since each rotation creates a new nested list via the comprehension.

Patterns

Rotation-by-enumeration: Rather than computing a rotation formula and checking all four analytically, the code iterates a loop 4 times, comparing then rotating. This is the idiomatic brute-force approach for small fixed rotation counts.

In-place rotation formula: The expression mat[n - 1 - j][i] is the standard 90-degree clockwise rotation of an n×n matrix. For a point at row i, column j in the rotated matrix, the source is row n-1-j, column i in the original. The outer list comprehension iterates i (rows of the result), the inner iterates j (columns of the result).

Early return: If any rotation matches, it returns True immediately without computing remaining rotations. The False at the end is only reached after all four rotations (0°, 90°, 180°, 270°) fail to match.

Dependencies

Imports: unittest (stdlib) for the test harness, List from typing for type annotations.

Imported by: The testsolution.py in this same directory, plus the "Imported By" list in the prompt appears to be auto-generated cross-references from the test runner infrastructure — those are unrelated test files, likely an artifact of the repo's shared test harness (runtests.py), not actual imports of this module.

Flow

1. Capture n = len(mat) — the matrix dimension.

2. Loop 4 times (covering 0°, 90°, 180°, 270°):

3. After all 4 checks fail, return False.

The rotation is applied *after* the equality check, so the first iteration checks the unrotated matrix (0°), and the last iteration's rotation result is never checked — which is correct, because a 360° rotation equals the original, already tested in iteration 0.

Invariants

Error Handling

None. The function trusts its inputs conform to the LeetCode contract (binary square matrices of equal size). No validation, no exceptions raised. Python's list equality handles the comparison, and the list comprehension handles the rotation — both would raise IndexError on malformed input, but that's not guarded against.