Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created December 21, 2025 22:50
Show Gist options
  • Select an option

  • Save Ifihan/55f5164cd9bdb038eab2149248e088b9 to your computer and use it in GitHub Desktop.

Select an option

Save Ifihan/55f5164cd9bdb038eab2149248e088b9 to your computer and use it in GitHub Desktop.
Delete Columns to Make Sorted II

Question

Approach

I process the strings column by column from left to right. I keep track of which adjacent string pairs are already confirmed to be in correct lexicographic order.

For each column:

  • I check all unconfirmed adjacent pairs.
  • If any pair violates lexicographic order (strs[i][c] > strs[i+1][c]), I must delete this column.
  • If the column is safe, I mark pairs where strs[i][c] < strs[i+1][c] as confirmed.

Implementation

Complexities

  • Time: O(n x m)
  • Space: O(n)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment