You are given an array of n
strings strs
, all of the same lengths. The strings can be arranged such that there is one on each line, making a grid. For example, strs = ["abc", "bce", "cae"]
can be arranged as:
1 2 3 4 |
abc bce cae You want to <strong>delete</strong> the columns that are <strong>not sorted lexicographically</strong>. In the above example (0-indexed), columns 0 (<code>'a'</code>, <code>'b'</code>, <code>'c'</code>) and 2 (<code>'c'</code>, <code>'e'</code>, <code>'e'</code>) are sorted while column 1 (<code>'b'</code>, <code>'c'</code>, <code>'a'</code>) is not, so you would delete column 1. Return <em>the number of columns that you will delete</em>. |
https://leetcode.com/problems/delete-columns-to-make-sorted/description/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Solution { public int minDeletionSize(String[] strs) { int count = 0; int n = strs.length, m = strs[0].length(); for(int col=0;col<m;col++){ for(int row = 1;row<n;row++){ if(strs[row-1].charAt(col) > strs[row].charAt(col)){ count++; break; } } } return count; } } |