You are given an array of n strings strs, all of the same length. The strings can be arranged so that there is one on each line, making a grid. For example, strs = ["abc", "bce", "cae"] is arranged as rows abc, bce, cae. You want to delete the columns that are not sorted lexicographically from top to bottom. In the example, columns 0 (a, b, c) and 2 (c, e, e) are sorted, while column 1 (b, c, a) is not, so you would delete column 1. Return the number of columns that you will delete.
You are given an array of n strings strs, all of the same length. The strings can be arranged so that there is one on each line, making a grid. For example, strs = ["abc", "bce", "cae"] is arranged as rows abc, bce, cae. You want to delete the columns that are not sorted lexicographically from top to bottom. In the example, columns 0 (a, b, c) and 2 (c, e, e) are sorted, while column 1 (b, c, a) is not, so you would delete column 1. Return the number of columns that you will delete.
Example 1:
Input: {"strs":["cba","daf","ghi"]}
Output: 1
Constraints:
n==strs.length
1≤n≤100
1≤strs[i].length≤1000
strs[i] consists of lowercase English letters.
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"strs":["cba","daf","ghi"]}
Stack the 3 strings into a grid with 3 columns. A column survives only if its letters never go down from top to bottom.