Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 of answer.length.
Example 1:
Input: strs = ["ca","bb","ac"] Output: 1 Explanation: After deleting the first column, strs = ["a", "b", "c"]. Now strs is in 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 is 1.
Example 2:
Input: strs = ["xc","yb","za"] Output: 0 Explanation: 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 true that (strs[0][0] <= strs[0][1] <= ...)
Example 3:
Input: strs = ["zyx","wvu","tsr"] Output: 3 Explanation: We have to delete every column.
Constraints:
n == strs.length1 <= n <= 1001 <= strs[i].length <= 100strs[i] consists of lowercase English letters.Problem summary: 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 of answer.length.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
["ca","bb","ac"]
["xc","yb","za"]
["zyx","wvu","tsr"]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #955: Delete Columns to Make Sorted II
class Solution {
public int minDeletionSize(String[] strs) {
int n = strs.length;
int m = strs[0].length();
boolean[] st = new boolean[n - 1];
int ans = 0;
for (int j = 0; j < m; ++j) {
boolean mustDel = false;
for (int i = 0; i < n - 1; ++i) {
if (!st[i] && strs[i].charAt(j) > strs[i + 1].charAt(j)) {
mustDel = true;
break;
}
}
if (mustDel) {
++ans;
} else {
for (int i = 0; i < n - 1; ++i) {
if (!st[i] && strs[i].charAt(j) < strs[i + 1].charAt(j)) {
st[i] = true;
}
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #955: Delete Columns to Make Sorted II
func minDeletionSize(strs []string) int {
n := len(strs)
m := len(strs[0])
st := make([]bool, n-1)
ans := 0
for j := 0; j < m; j++ {
mustDel := false
for i := 0; i < n-1; i++ {
if !st[i] && strs[i][j] > strs[i+1][j] {
mustDel = true
break
}
}
if mustDel {
ans++
} else {
for i := 0; i < n-1; i++ {
if !st[i] && strs[i][j] < strs[i+1][j] {
st[i] = true
}
}
}
}
return ans
}
# Accepted solution for LeetCode #955: Delete Columns to Make Sorted II
class Solution:
def minDeletionSize(self, strs: List[str]) -> int:
n = len(strs)
m = len(strs[0])
st = [False] * (n - 1)
ans = 0
for j in range(m):
must_del = False
for i in range(n - 1):
if not st[i] and strs[i][j] > strs[i + 1][j]:
must_del = True
break
if must_del:
ans += 1
else:
for i in range(n - 1):
if not st[i] and strs[i][j] < strs[i + 1][j]:
st[i] = True
return ans
// Accepted solution for LeetCode #955: Delete Columns to Make Sorted II
impl Solution {
pub fn min_deletion_size(strs: Vec<String>) -> i32 {
let n = strs.len();
let m = strs[0].len();
let mut st = vec![false; n - 1];
let mut ans = 0;
for j in 0..m {
let mut must_del = false;
for i in 0..n - 1 {
if !st[i] && strs[i].as_bytes()[j] > strs[i + 1].as_bytes()[j] {
must_del = true;
break;
}
}
if must_del {
ans += 1;
} else {
for i in 0..n - 1 {
if !st[i] && strs[i].as_bytes()[j] < strs[i + 1].as_bytes()[j] {
st[i] = true;
}
}
}
}
ans
}
}
// Accepted solution for LeetCode #955: Delete Columns to Make Sorted II
function minDeletionSize(strs: string[]): number {
const n = strs.length;
const m = strs[0].length;
const st: boolean[] = Array(n - 1).fill(false);
let ans = 0;
for (let j = 0; j < m; j++) {
let mustDel = false;
for (let i = 0; i < n - 1; i++) {
if (!st[i] && strs[i][j] > strs[i + 1][j]) {
mustDel = true;
break;
}
}
if (mustDel) {
ans++;
} else {
for (let i = 0; i < n - 1; i++) {
if (!st[i] && strs[i][j] < strs[i + 1][j]) {
st[i] = true;
}
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.