File: delete-columns-to-make-sorted/solution.py

Date: 2026-06-06

Time: 16:15

delete-columns-to-make-sorted/solution.py

Purpose

This file solves LeetCode 944: Delete Columns to Make Sorted. Given a list of equal-length strings (imagine them as rows of a grid), it counts how many columns are not sorted in non-decreasing lexicographic order. Each such column would need to be "deleted" to make every remaining column sorted top-to-bottom.

Key Components

Solution.minDeletionSize(strs: List[str]) -> int — The sole method. It iterates column-by-column over the character grid formed by strs, and for each column checks whether any adjacent pair of characters violates non-decreasing order. If so, that column is counted and immediately skipped via break.

Patterns

Column-major traversal with early exit. The outer loop walks columns (j), the inner loop walks rows (i). The break on the first out-of-order pair avoids unnecessary comparisons in the rest of the column — once you know a column is unsorted, you don't need to keep checking.

Grid-as-strings idiom. Rather than converting to a 2D array, the code indexes directly into each string with strs[i][j]. This is standard for LeetCode string-grid problems and avoids allocation.

Dependencies

Flow

1. Initialize count = 0.

2. For each column index j in 0..len(strs[0]):

3. Return count.

Invariants

Error Handling

None. The code trusts the LeetCode contract: strs is a non-empty list of equal-length, non-empty, lowercase-letter strings. An empty strs would raise IndexError at len(strs[0]).

Complexity

Beliefs