#944

Delete Columns to Make Sorted

newbie · 270 · lc easy +19 · verified · 78.1% accepted · 2,059 likes · top 89%

Description

Arrange the n equal-length strings strs as rows of a character grid. A column must be deleted if its characters, read top to bottom, are not in non-decreasing order. Return the count of columns that must be deleted.

Example 1:

abc
bce
cae

Example 2:

Input: strs = ["cba","daf","ghi"]
Output: 1
Explanation: The grid looks as follows:
cba
daf
ghi
Columns 0 and 2 are sorted, but column 1 is not, so you only need to delete 1 column.

Example 3:

Input: strs = ["a","b"]
Output: 0
Explanation: The grid looks as follows:
a
b
Column 0 is the only column and is sorted, so you will not delete any columns.

Example 4:

Input: strs = ["zyx","wvu","tsr"]
Output: 3
Explanation: The grid looks as follows:
zyx
wvu
tsr
All 3 columns are not sorted, so you will delete all 3.

Code

1
2
3