Easy to understand with explanation
Intuition\n Describe your first thoughts on how to solve this problem. \nTo solve this problem, we need to go column by column in the strings, checking which co
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo solve this problem, we need to go column by column in the strings, checking which columns need to be removed to ensure that all rows remain in lexicographical order.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nExplanation:\nis_sorted: This array keeps track of whether adjacent rows are already sorted due to previous columns.\nIf is_sorted[i] is True, it means strs[i] <= strs[i + 1] based on earlier columns, so we don\'t need to compare those two strings anymore.\nFor each column:\nIf any row pair (that isn\'t already sorted) violates lexicographical order in the current column, mark the column for deletion (remove_column), then break out of the inner loop to avoid unnecessary comparisons.\nIf the column is not marked for deletion, update is_sorted based on the current column.\nBenefits:\nThis optimization ensures that once adjacent rows are confirmed to be sorted, they won\'t be compared again in future columns, reducing the number of comparisons.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n * m)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n * m)\n# Code\n```python3 []\nclass Solution:\n    def minDeletionSize(self, strs: List[str]) -> int:\n        n, m = len(strs), len(strs[0])\n        delete_count = 0\n        is_sorted = [False] * (n - 1)  # Tracks if consecutive pairs are already sorted\n\n        for col in range(m):\n            # Check if current column maintains order for pairs that are not already sorted\n            remove_column = False\n            for row in range(1, n):\n                if not is_sorted[row - 1] and strs[row][col] < strs[row - 1][col]:\n                    remove_column = True\n                    break\n            \n            if remove_column:\n                delete_count += 1\n            else:\n                # Mark rows as sorted if they become sorted due to this column\n                for row in range(1, n):\n                    if strs[row][col] > strs[row - 1][col]:\n                        is_sorted[row - 1] = True\n\n        return delete_count\n```
['Python3']