Delete Columns to Make Sorted (LeetCode 944): Solution and Explanation
Stack the strings into a grid, read each column from top to bottom, and count the columns where some letter is smaller than the letter above it.
Intuition
Writing the strings one per line turns the input into a grid. The question is about columns, and each column can be judged without looking at any other column.
A column is sorted when its letters never go down. That is a property of neighbouring pairs: the column is unsorted exactly when some letter is smaller than the one directly above it.
So for each column, walk down comparing neighbours, and the first descent is proof enough to delete it.
Approach: Column scan with early exit
- Set deleted = 0.
- For each column c, walk rows r = 1 to n - 1.
- If strs[r][c] < strs[r - 1][c], add 1 to deleted and stop checking this column.
- Return deleted.
Step-by-step example
strs = ["cba","daf","ghi"]
- Column 0 reads c, d, g. c <= d and d <= g, so it is sorted and stays.
- Column 1 reads b, a, h. a < b already at row 1, so delete it. deleted = 1.
- Column 2 reads a, f, i. Sorted, it stays.
- The answer is 1.
Code: Python, Java, C++
1class Solution:
2 def minDeletionSize(self, strs: List[str]) -> int:
3 deleted = 0
4 for c in range(len(strs[0])):
5 for r in range(1, len(strs)):
6 if strs[r][c] < strs[r - 1][c]:
7 deleted += 1
8 break
9 return deleted1class Solution {
2 public int minDeletionSize(String[] strs) {
3 int deleted = 0;
4 for (int c = 0; c < strs[0].length(); c++) {
5 for (int r = 1; r < strs.length; r++) {
6 if (strs[r].charAt(c) < strs[r - 1].charAt(c)) {
7 deleted++;
8 break;
9 }
10 }
11 }
12 return deleted;
13 }
14}1class Solution {
2public:
3 int minDeletionSize(vector<string>& strs) {
4 int deleted = 0;
5 for (int c = 0; c < (int)strs[0].size(); c++) {
6 for (int r = 1; r < (int)strs.size(); r++) {
7 if (strs[r][c] < strs[r - 1][c]) {
8 deleted++;
9 break;
10 }
11 }
12 }
13 return deleted;
14 }
15};Time and space complexity
O(n * m)
Each of the m columns is scanned over at most n rows, and every cell is read at most once.
O(1)
Only the counter and loop indices are stored.
Other approaches
Transpose and compare with sorted (time O(m * n log n), space O(n))
Build each column as a string and compare it with its sorted copy. Easy to read, but it allocates and sorts every column.
Common mistakes
- Iterating rows in the outer loop. You can still get the right count, but you need a per-column flag so a column is not counted twice.
- Using <= instead of <. Equal neighbours like a, a are allowed in a sorted column.
- Checking whether whole rows are sorted. The problem asks about columns read top to bottom.
Frequently asked questions
Why can I stop at the first descent?
One descent already makes the column unsorted, and a column is counted once whatever happens below. Further checks cannot change the answer.
What if there is only one string?
Every column has a single letter, which is trivially sorted, so the answer is 0.
Do I actually have to delete anything?
No. The problem only asks how many columns would be deleted, so counting is enough.