Delete Columns to Make Sorted — LeetCode 944 Python Solution
EasyArrayString
- Problem
- #944
- Pattern
- Hash Map
- Reading time
- 2 min
- Source
- leetcode.com
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
abc bce cae
Python solution
Python
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 ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(L), where L is the total length of the strings in the array \textit{strs} |
| Space | O(1) auxiliary |
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.