<p>You are given an array of <code>n</code> strings <code>strs</code>, all of the same length.</p>

<p>We may choose any deletion indices, and we delete all the characters in those indices for each string.</p>

<p>For example, if we have <code>strs = [&quot;abcdef&quot;,&quot;uvwxyz&quot;]</code> and deletion indices <code>{0, 2, 3}</code>, then the final array after deletions is <code>[&quot;bef&quot;, &quot;vyz&quot;]</code>.</p>

<p>Suppose we chose a set of deletion indices <code>answer</code> such that after deletions, the final array has <strong>every string (row) in lexicographic</strong> order. (i.e., <code>(strs[0][0] &lt;= strs[0][1] &lt;= ... &lt;= strs[0][strs[0].length - 1])</code>, and <code>(strs[1][0] &lt;= strs[1][1] &lt;= ... &lt;= strs[1][strs[1].length - 1])</code>, and so on). Return <em>the minimum possible value of</em> <code>answer.length</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> strs = [&quot;babca&quot;,&quot;bbazb&quot;]
<strong>Output:</strong> 3
<strong>Explanation:</strong> After deleting columns 0, 1, and 4, the final array is strs = [&quot;bc&quot;, &quot;az&quot;].
Both these rows are individually in lexicographic order (ie. strs[0][0] &lt;= strs[0][1] and strs[1][0] &lt;= strs[1][1]).
Note that strs[0] &gt; strs[1] - the array strs is not necessarily in lexicographic order.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> strs = [&quot;edcba&quot;]
<strong>Output:</strong> 4
<strong>Explanation:</strong> If we delete less than 4 columns, the only row will not be lexicographically sorted.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> strs = [&quot;ghi&quot;,&quot;def&quot;,&quot;abc&quot;]
<strong>Output:</strong> 0
<strong>Explanation:</strong> All rows are already lexicographically sorted.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == strs.length</code></li>
	<li><code>1 &lt;= n &lt;= 100</code></li>
	<li><code>1 &lt;= strs[i].length &lt;= 100</code></li>
	<li><code>strs[i]</code> consists of lowercase English letters.</li>
</ul>

<ul>
	<li>&nbsp;</li>
</ul>

class Solution {
public:
    int dp[103][103];
    vector<string> strs;
    int n;
    
    // Recursive function with memoization
    int func(int ind, int prev) {
        if (ind == n) return 0;  // Base case: end of columns
        
        int &ans = dp[ind][prev + 1];  // Memoized result reference
        if (ans != -1) return ans;  // Return if already computed
        
        ans = 1+func(ind + 1, prev);  // Case 1: Skip this column and move forward

        bool flag = false;
        for (int i = 0; i < strs.size(); i++) {
            // If a previous column exists (prev != -1) and order is violated
            if (prev != -1 && strs[i][ind] < strs[i][prev]) {
                flag = true;
                break;
            }
        }

        // If no violations in the order, try including this column
        if (!flag) {
            ans = min(ans,func(ind + 1, ind));  // Case 2: Delete this column and continue
        }

        return ans;  // Return the best option
    }

    // Main function to minimize deletion size
    int minDeletionSize(vector<string>& strs) {
        memset(dp, -1, sizeof(dp));  // Initialize DP table to -1
        this->strs = strs;
        this->n = strs[0].size();  // Set the number of columns (length of the strings)
        return func(0, -1);  // Start recursion from column 0 with no previous column
    }
};


13900