Created
December 20, 2025 21:30
-
-
Save tatsuyax25/cf611ec1c3fcf43496d118767d8e6e73 to your computer and use it in GitHub Desktop.
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. For example, strs = ["abc", "bce", "cae"] can be arranged as follows:
abc
bce
cae
You want to delete t
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {string[]} strs | |
| * @return {number} | |
| */ | |
| var minDeletionSize = function(strs) { | |
| // Number of rows (strings) | |
| const n = strs.length; | |
| // Number of columns (length of each string) | |
| const m = strs[0].length; | |
| let deletions = 0; | |
| // We check each column independently | |
| for (let col = 0; col < m; col++) { | |
| // For each column, scan down the row | |
| for (let row = 1; row < n; row++) { | |
| // Compare current row with previous row | |
| // If strs[row][col] < strs[row-1][col], the column is unsorted | |
| if (strs[row][col] < strs[row - 1][col]) { | |
| deletions++; | |
| break; // No need to check further rows in this column | |
| } | |
| } | |
| } | |
| return deletions; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment