LeetCode 944: Delete Columns to Make Sorted

ArrayString

Problem Description

Explanation

To solve this problem, we need to iterate through each column of the grid and check if the characters in that column are sorted lexicographically. If any column is not sorted, we increment a counter. The final count of columns that are not sorted will be our answer.

  • Time complexity: O(n * m), where n is the number of strings and m is the length of each string.
  • Space complexity: O(1)

Solutions

class Solution {
    public int minDeletionSize(String[] strs) {
        int count = 0;
        for (int i = 0; i < strs[0].length(); i++) {
            for (int j = 1; j < strs.length; j++) {
                if (strs[j].charAt(i) < strs[j - 1].charAt(i)) {
                    count++;
                    break;
                }
            }
        }
        return count;
    }
}

Loading editor...