> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/dynamic-programming/delete-columns-to-make-sorted-iii.md).

# Delete Columns to Make Sorted III

We are given an array `A` of `N` lowercase letter strings, all of the same length.

Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.

For example, if we have an array `A = ["babca","bbazb"]` and deletion indices `{0, 1, 4}`, then the final array after deletions is `["bc","az"]`.

Suppose we chose a set of deletion indices `D` such that after deletions, the final array has **every element (row) in lexicographic** order.

For clarity, `A[0]` is in lexicographic order (ie. `A[0][0] <= A[0][1] <= ... <= A[0][A[0].length - 1]`), `A[1]` is in lexicographic order (ie. `A[1][0] <= A[1][1] <= ... <= A[1][A[1].length - 1]`), and so on.

Return the minimum possible value of `D.length`.

**Example 1:**

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

**Example 2:**

```
Input: ["edcba"]
Output: 4
Explanation: If we delete less than 4 columns, the only row won't be lexicographically sorted.
```

**Example 3:**

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

**Note:**

1. `1 <= A.length <= 100`
2. `1 <= A[i].length <= 100`

```java
class Solution {
    public int minDeletionSize(String[] A) {
        int m = A.length, n = A[0].length(), res = n - 1, k;
        int[] dp = new int[n];
        // dp[i] -> holds longest increasing subsequence length, which is common for all
        // the strings(index wise), then the answer will be total - LIS (basically we
        // will delete all the remaining columns to get lexico order)
        Arrays.fill(dp, 1);
        // For each position i,we track the maximum increasing subsequence.
        // To do that, we analyze all j < i
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < i; ++j) {
                for (k = 0; k < m; ++k)
                    if (A[k].charAt(j) > A[k].charAt(i))
                        break;
                // if A[j] < A[i] for all strings
                if (k == m && dp[j] + 1 > dp[i])
                    dp[i] = dp[j] + 1;
            }
            res = Math.min(res, n - dp[i]);
        }
        return res;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/dynamic-programming/delete-columns-to-make-sorted-iii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
