Delete Columns to Make Sorted

Easy

Description

Given an array of n strings strs, all of the same length, find the number of columns that are not sorted lexicographically. Delete those columns.

Examples

Input:strs = ["cba","daf","ghi"]
Output:1
Explanation:

Column 1 unsorted.

Input:strs = ["a","b"]
Output:0
Explanation:

Each string has only one column, and that column ('a','b') is already sorted. No columns need to be deleted.

Input:strs = ["abc","def","ghi"]
Output:0
Explanation:

All three columns are sorted: column 0 has 'a'<'d'<'g', column 1 has 'b'<'e'<'h', and column 2 has 'c'<'f'<'i'. No columns need to be deleted.

Constraints

  • 1 ≤ n ≤ 100

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!