You are given an array of n strings strs, all of the same length.
We may choose any deletion indices, and we delete all the characters in those indices for each string.
For example, if we have strs = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef", "vyz"].
Suppose we chose a set of deletion indices answer such that after deletions, the final array has its elements in lexicographic order (i.e., strs[0] <= strs[1] <= strs[2] <= ... <= strs[n - 1]). Return the minimum possible value ofanswer.length.
Input: strs =["ca","bb","ac"]Output: 1Explanation:
After deleting the first column, strs =["a","b","c"].Now strs isin lexicographic order(ie. strs[0]<= strs[1]<= strs[2]).We require at least 1 deletion since initially strs was not in lexicographic order, so the answer is1.
Input: strs =["xc","yb","za"]Output: 0Explanation:
strs is already in lexicographic order, so we do not need to delete anything.Note that the rows of strs are not necessarily in lexicographic order:i.e., it is NOT necessarily truethat(strs[0][0]<= strs[0][1]<=...)
The key idea is to process columns from left to right, greedily deleting columns that break the lexicographical order between any two adjacent strings. We only keep columns that do not cause any disorder, and once a pair of strings is confirmed to be ordered, we never need to check them again.
classSolution {
public:int minDeletionSize(vector<string>& strs) {
int n = strs.size(), m = strs[0].size(), ans =0;
vector<bool> sorted(n -1, false);
for (int col =0; col < m; ++col) {
bool needDel = false;
for (int i =0; i < n -1; ++i) {
if (!sorted[i] && strs[i][col] > strs[i +1][col]) {
needDel = true;
break;
}
}
if (needDel) {
++ans;
continue;
}
for (int i =0; i < n -1; ++i) {
if (strs[i][col] < strs[i +1][col])
sorted[i] = true;
}
}
return ans;
}
};
classSolution {
publicintminDeletionSize(String[] strs) {
int n = strs.length, m = strs[0].length(), ans = 0;
boolean[] sorted =newboolean[n - 1];
for (int col = 0; col < m; col++) {
boolean needDel =false;
for (int i = 0; i < n - 1; i++) {
if (!sorted[i]&& strs[i].charAt(col) > strs[i + 1].charAt(col)) {
needDel =true;
break;
}
}
if (needDel) {
ans++;
continue;
}
for (int i = 0; i < n - 1; i++) {
if (strs[i].charAt(col) < strs[i + 1].charAt(col)) {
sorted[i]=true;
}
}
}
return ans;
}
}
classSolution {
funminDeletionSize(strs: Array<String>): Int {
val n = strs.size
val m = strs[0].length
var ans = 0val sorted = BooleanArray(n - 1)
for (col in0 until m) {
var needDel = falsefor (i in0 until n - 1) {
if (!sorted[i] && strs[i][col] > strs[i + 1][col]) {
needDel = truebreak }
}
if (needDel) {
ans++continue }
for (i in0 until n - 1) {
if (strs[i][col] < strs[i + 1][col]) {
sorted[i] = true }
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution:
defminDeletionSize(self, strs: list[str]) -> int:
n, m = len(strs), len(strs[0])
ans =0 sorted_pairs = [False] * (n -1)
for col in range(m):
need_del =Falsefor i in range(n -1):
ifnot sorted_pairs[i] and strs[i][col] > strs[i +1][col]:
need_del =Truebreakif need_del:
ans +=1continuefor i in range(n -1):
if strs[i][col] < strs[i +1][col]:
sorted_pairs[i] =Truereturn ans
impl Solution {
pubfnmin_deletion_size(strs: Vec<String>) -> i32 {
let n = strs.len();
let m = strs[0].len();
letmut ans =0;
letmut sorted =vec![false; n -1];
let strs: Vec<Vec<u8>>= strs.into_iter().map(|s| s.into_bytes()).collect();
for col in0..m {
letmut need_del =false;
for i in0..n -1 {
if!sorted[i] && strs[i][col] > strs[i +1][col] {
need_del =true;
break;
}
}
if need_del {
ans +=1;
continue;
}
for i in0..n -1 {
if strs[i][col] < strs[i +1][col] {
sorted[i] =true;
}
}
}
ans
}
}