LeetCode 955: Delete Columns to Make Sorted II Solution

Master LeetCode problem 955 (Delete Columns to Make Sorted II), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

955. Delete Columns to Make Sorted II

Problem Explanation

Explanation:

To solve this problem, we need to find the minimum number of columns that need to be deleted in order to make all strings sorted lexicographically. We can achieve this by iterating through each column and checking if the current column violates the lexicographic order. If a violation is found, we increment a counter representing the number of columns that need to be deleted.

  • Initialize a counter variable to 0.
  • Iterate through each column.
  • Check if the current column violates the lexicographic order.
  • If a violation is found, increment the counter.
  • Return the counter as the minimum number of columns to be deleted.

Time Complexity: O(n * m) where n is the number of strings and m is the length of each string.

Space Complexity: O(1)

:

Solution Code

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

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 955 (Delete Columns to Make Sorted II)?

This page provides optimized solutions for LeetCode problem 955 (Delete Columns to Make Sorted II) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 955 (Delete Columns to Make Sorted II)?

The time complexity for LeetCode 955 (Delete Columns to Make Sorted II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 955 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 955 in Java, C++, or Python.

Back to LeetCode Solutions