Skip to main content
LeetCode 944, Easy. Topics: Array, String, Greedy. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 24 parametrized pytest cases, and a playground notebook:

Problem

You are given an array of n strings strs, all of the same length. The strings can be arranged such that there is one on each line, making a grid.
  • For example, strs = ["abc", "bce", "cae"] can be arranged as follows:
You want to delete the columns that are not sorted lexicographically. In the above example (0-indexed), columns 0 ('a', 'b', 'c') and 2 ('c', 'e', 'e') are sorted, while column 1 ('b', 'c', 'a') is not, so you would delete column 1. Return the number of columns that you will delete.

Examples

Constraints

  • n == strs.length
  • 1 <= n <= 100
  • 1 <= strs[i].length <= 1000
  • strs[i] consists of lowercase English letters.

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

Last modified on September 7, 2026