Delete Columns to Make Sorted II - Problem
You are given an array of n strings strs, all of the same length.
We may choose any deletion indices, and we delete all the characters in those indices for each string.
For example, if we have strs = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef", "vyz"].
Suppose we chose a set of deletion indices answer such that after deletions, the final array has its elements in lexicographic order (i.e., strs[0] <= strs[1] <= strs[2] <= ... <= strs[n - 1]).
Return the minimum possible value of answer.length.
Input & Output
Example 1 — Basic Case
$
Input:
strs = ["ca", "bb", "ac"]
›
Output:
1
💡 Note:
Delete column 0: "ca"→"a", "bb"→"b", "ac"→"c". Result ["a","b","c"] is sorted. Only 1 deletion needed.
Example 2 — No Deletions Needed
$
Input:
strs = ["xc", "yb", "za"]
›
Output:
0
💡 Note:
Already lexicographically sorted: "xc" <= "yb" <= "za". No deletions needed.
Example 3 — Multiple Deletions
$
Input:
strs = ["zyx", "wvu", "tsr"]
›
Output:
3
💡 Note:
Must delete all columns since each column is in reverse order. After deleting all, get ["","",""] which is sorted.
Constraints
- 1 ≤ strs.length ≤ 100
- 1 ≤ strs[i].length ≤ 100
- strs[i] consists of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of strings with same length
2
Process
Delete minimum columns to achieve lexicographic order
3
Output
Minimum number of deletions needed
Key Takeaway
🎯 Key Insight: Once string pairs are lexicographically separated by a column, their order is permanently established
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code