# Delete Columns To Make Sorted III Problem

## Description

LeetCode Problem 960.

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 every string (row) in lexicographic order. (i.e., (strs <= strs <= … <= strs[strs.length - 1]), and (strs <= strs <= … <= strs[strs.length - 1]), and so on). Return the minimum possible value of answer.length.

Example 1:

``````1
2
3
4
5
Input: strs = ["babca","bbazb"]
Output: 3
Explanation: After deleting columns 0, 1, and 4, the final array is strs = ["bc", "az"].
Both these rows are individually in lexicographic order (ie. strs <= strs and strs <= strs).
Note that strs > strs - the array strs is not necessarily in lexicographic order.
``````

Example 2:

``````1
2
3
Input: strs = ["edcba"]
Output: 4
Explanation: If we delete less than 4 columns, the only row will not be lexicographically sorted.
``````

Example 3:

``````1
2
3
Input: strs = ["ghi","def","abc"]
Output: 0
Explanation: All rows are already lexicographically sorted.
``````

Constraints:

• n == strs.length
• 1 <= n <= 100
• 1 <= strs[i].length <= 100
• strs[i] consists of lowercase English letters.

## Sample C++ Code

``````1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int minDeletionSize(vector<string>& A) {
int m = A.size();
int n = A.size();
vector<int> dp(A.size(), 1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (cmp(A, i, j)) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
}
return n - *max_element(dp.begin(), dp.end());
}

bool cmp(vector<string> &arr, int i, int j) {
for (int k = 0; k < arr.size(); k++) {
if (arr[k][j] > arr[k][i])
return 0;
}
return 1;
}
};
``````