EasyNo. 0944

Delete Columns to Make Sorted — LeetCode 944 Python Solution

Pattern
Hash Map
Topics
Array, String
Reading time
2 min

The problem

You are given an array of n strings strs, all of the same length. The strings can be arranged such that there is one on each line, making a grid.

Example

Example
abc
bce
cae

Python solution

PythonSolution
class Solution:
    def minDeletionSize(self, strs: List[str]) -> int:
        m, n = len(strs[0]), len(strs)
        ans = 0
        for j in range(m):
            for i in range(1, n):
                if strs[i][j] < strs[i - 1][j]:
                    ans += 1
                    break
        return ans

Complexity

Time
O(L)where L is the total length of the strings in the array strs
Space · auxiliary
O(1)

Pattern: Hash Map

Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 944. Delete Columns to Make Sorted is filed here because the reference solution below belongs to the algorithm family this hub collects, even though its LeetCode tags point elsewhere.

The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 944. Delete Columns to Make Sorted?

LeetCode 944. Delete Columns to Make Sorted is rated Easy on LeetCode.

What is the time complexity of LeetCode 944. Delete Columns to Make Sorted?

The Python solution on this page runs in O(L), where L is the total length of the strings in the array \textit{strs}.

What is the space complexity of LeetCode 944. Delete Columns to Make Sorted?

The Python solution on this page uses O(1) auxiliary space.

What topics does LeetCode 944. Delete Columns to Make Sorted cover?

LeetCode 944. Delete Columns to Make Sorted is tagged Array and String on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.