Ctrl K

Count the Unsorted Columns

Easy
You are given an array rows of strings that all have the same length. Written one under another, they form a grid of letters. A column is sorted if its letters, read top to bottom, are in alphabetical order (equal neighbours are allowed). Return how many columns of the grid are **not** sorted.
Example 1
rows=['bad', 'cab', 'dog'] → 1

Reading top to bottom, the columns are "bcd", "aao" and "dbg". Only the last one is out of order.

Example 2
rows=['zy', 'ax'] → 2

The first column goes z → a and the second y → x, so both break alphabetical order.

Example 3
rows=['q'] → 0

A column with a single letter is always in order.

Constraints
  • 1 ≤ len(rows) ≤ 100
  • 1 ≤ len(rows[i]) ≤ 1000
  • All strings have the same length
  • The strings consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Words, Closest Apart EasyOne-Row Typing EasyDinner for Two EasyPhrases with Synonyms EasyWord from a License Plate Easy
loading Python…
Input
rows=['bad', 'cab', 'dog']
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 93 tests on the server, hidden ones included.