#944
Easy Algorithms Delete columns to make sorted
Array String
78.1% acceptance
Feb 25, 2026
2057
3014
You are given an array of n strings strs, all of the same length.
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 follows:
abc
bce
cae
You want to delete the columns that are not sorted lexicographically. In the above example (0-indexed), 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.
Solution
Rust
Time O(n²)
Space O(1)
impl Solution {
pub fn min_deletion_size(strs: Vec<String>) -> i32 {
let cols = strs[0].len();
let rows = strs.len();
let strs: Vec<Vec<u8>> = strs.iter().map(|s| s.bytes().collect()).collect();
let mut count = 0;
for c in 0..cols {
for r in 1..rows {
if strs[r][c] < strs[r-1][c] {
count += 1;
break;
}
}
}
count
}
}