Delete Columns to Make Sorted II

You are given an array of n strings strs, all of the same length.

You may choose any deletion indices, and you delete all the characters in those indices for each string.

For example, if strs = ["abcdef", "uvwxyz"] and the deletion indices are {0, 2, 3}, then the final array after deletions is ["bef", "vyz"].

Suppose you chose a set of deletion indices answer such that after deletions, the final array has its elements in lexicographic order, meaning strs[0] <= strs[1] <= strs[2] <= ... <= strs[n - 1]. Return the minimum possible value of answer.length.

Example 1
Inputstrs = ["ca","bb","ac"]
Output1
After deleting the first column, strs becomes ["a", "b", "c"], which is lexicographically sorted, and at least one deletion is required because the original array was not sorted.
Example 2
Inputstrs = ["xc","yb","za"]
Output0
The array is already in lexicographic order, so no deletions are needed.

Constraints

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

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate