Delete Columns to Make Sorted

EasyStringssimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given strs, a list of n lowercase strings that all have the same length. Stack them vertically so that the characters at index j of every string form column j. A column is **sorted** if its characters, read from top (first string) to bottom (last string), never decrease in alphabetical order. Equal adjacent characters are fine. Return the number of columns that are **not** sorted, i.e. how many columns you would have to delete so that every remaining column is sorted.

Examples

Input: strs = ["cba","daf","ghi"]
Output: 1
Columns read `cdg`, `bah`, `afi`. Only `bah` goes down (`b` > `a`), so one column must go.
Input: strs = ["a","b"]
Output: 0
The single column `ab` is already sorted.
Input: strs = ["zyx","wvu","tsr"]
Output: 3
Every column strictly decreases from top to bottom.

Constraints

  • 1 <= strs.length <= 100
  • 1 <= strs[i].length <= 1000
  • All strings in `strs` have the same length
  • `strs[i]` consists of lowercase English letters

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems