Delete Columns to Make Sorted II - Problem

You are given an array of n strings strs, where all strings have the same length. Your task is to delete columns (character positions) to make the strings appear in lexicographic order.

When you delete a column at index i, you remove the character at position i from all strings. For example, if we have strs = ["abcdef", "uvwxyz"] and delete columns {0, 2, 3}, the result is ["bef", "vyz"].

The goal is to find the minimum number of columns to delete so that the remaining strings are in lexicographic order: strs[0] ≤ strs[1] ≤ strs[2] ≤ ... ≤ strs[n-1].

Key insight: Unlike the simpler version of this problem, we need to be smart about which columns to keep. Once we establish that one string is definitively smaller than another at some position, we don't need to worry about those strings in future column checks.

Input & Output

example_1.py — Basic Case
$ Input: ["ca","bb","ac"]
Output: 1
💡 Note: We need to delete column 0. After deletion, strings become ["a","b","c"] which are in lexicographic order. Column 1 alone gives us the sorted result.
example_2.py — Multiple Deletions
$ Input: ["xc","yb","za"]
Output: 0
💡 Note: The strings are already in lexicographic order ["xc","yb","za"] since 'xc' < 'yb' < 'za'. No deletions needed.
example_3.py — All Deletions
$ Input: ["zyx","wvu","tsr"]
Output: 3
💡 Note: No matter which columns we keep, we cannot make these strings lexicographically sorted since z > w > t, y > v > s, and x > u > r. We must delete all columns, leaving empty strings which are trivially sorted.

Constraints

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

Visualization

Tap to expand
Delete Columns to Make Sorted - Visual FlowStep 1: Original StringscabbacStep 2: Check Column 0cbac > b ❌
DELETEStep 3: Check Column 1abca < b < c ✓
KEEP
ResultabcSorted!Algorithm Walkthrough1. Initialize: cut = [false, false] (track sorted pairs)2. Column 0: 'c' > 'b' breaks order → DELETE column3. Column 1: 'a' < 'b' < 'c' maintains order → KEEP4. Update: cut = [true, true] (all pairs now sorted)Result: 1 column deleted, strings are now ["a", "b", "c"]🎯 Time: O(n×m) | Space: O(n) | Optimal Greedy Solution
Understanding the Visualization
1
Initialize State
Set up tracking for which adjacent string pairs are definitively sorted
2
Process Each Column
For each column, check if keeping it maintains lexicographic order
3
Make Decision
Delete columns that break order, keep safe columns
4
Update Tracking
Mark newly established sorted relationships for future reference
Key Takeaway
🎯 Key Insight: Track definitively sorted pairs to avoid redundant comparisons - once two strings are ordered, they stay ordered!
Asked in
Google 42 Amazon 38 Facebook 25 Microsoft 18
28.5K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen