Problem Description
Given an array of n strings of the same length, form a grid where each string is a row. Determine the number of columns that are not in lexicographic order (top-to-bottom). For each column, if the characters are not sorted, then that column must be deleted.
Key Insights
- The grid can be viewed column by column.
- For each column, check if every character is less than or equal to the character in the same column of the next row.
- Count columns that fail the sorted order condition.
Space and Time Complexity
Time Complexity: O(n * m), where n is the number of strings and m is the length of each string. Space Complexity: O(1) extra space (ignoring input storage).
Solution
We iterate over each column index from 0 to m-1. For every column, we compare each character with the one right below it across all rows. If any character is greater than the one beneath it, that column is considered unsorted and we increment our delete counter. The solution uses a simple iterative approach with nested loops, ensuring that all characters in each column satisfy the sorted order condition.