Code Challenge #1
- Biyi Akinpelu
- Feb 2, 2023
- 1 min read
Updated: Nov 10, 2023
You are given an N by M 2D matrix of lowercase letters. Determine the minimum number of columns that can be removed to ensure that each row is ordered from top to bottom lexicographically. That is, the letter at each column is lexicographically later as you go down each row. It does not matter whether each row itself is ordered lexicographically.

For example, given the following table:
Example 1 |
---|
cba |
daf |
ghi |
This is not ordered because of the a in the center. We can remove the second column to make it ordered:
Example 2 |
ca |
df |
gi |
So your function should return 1, since we only needed to remove 1 column. As another example, given the following table:
Example 3 |
abcdef |
Your function should return 0, since the rows are already ordered (there's only one row).
As another example, given the following table:
Example 4 |
zyx |
wvu |
tsr |
Comments