Given an array **arr[]** of strings of same length, the task is to count the number of columns to be deleted so that all the rows are lexicographically sorted.

**Examples:**

Input:arr[] = {“hello”, “geeks”}

Output:1

Deleting column 1 (index 0)

Now both strings are sorted in lexicographical order i.e. “ello” and “eeks”.

Input:arr[] = {“xyz”, “lmn”, “pqr”}

Output:0

All rows are already sorted lexicographically.

**Approach:** The idea of the problem is to find the columns to keep, instead of columns to delete. Finally we return the difference of the counted value with the length of the string.

Now, let’s say we keep the first column **C1**. The next column **C2** we must have all rows lexicographically sorted i.e. **C1[i] <= C2[i]** for all valid values of **i** and we say that we have deleted all columns between **C1** and **C2**.

Below is the implementation of the above approach:

`# Python3 implementation of the approach ` ` ` `# Function to find minimum columns to be deleted ` `def` `deleteColumns(A): ` ` ` ` ` `# Length of each string ` ` ` `l ` `=` `len` `(A[` `0` `]) ` ` ` ` ` `# Initialize dp array ` ` ` `dp ` `=` `[` `1` `] ` `*` `l ` ` ` ` ` `for` `i ` `in` `range` `(l ` `-` `2` `, ` `-` `1` `, ` `-` `1` `): ` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, l): ` ` ` `if` `all` `(row[i] <` `=` `row[j] ` `for` `row ` `in` `A): ` ` ` `dp[i] ` `=` `max` `(dp[i], ` `1` `+` `dp[j]) ` ` ` ` ` `# Return result ` ` ` `return` `l ` `-` `max` `(dp) ` ` ` ` ` `# Driver Code ` `arr ` `=` `[` `"hello"` `, ` `"geeks"` `] ` ` ` `# Function call to print required answer ` `print` `(deleteColumns(arr)) ` |

*chevron_right*

*filter_none*

**Output:**

1

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum characters to be deleted from the beginning of two strings to make them equal
- Minimum characters to be deleted from the end to make given two strings equal
- Length of longest substring to be deleted to make a string equal to another string
- Print all elements in sorted order from row and column wise sorted matrix
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Program to find the Sum of each Row and each Column of a Matrix
- Minimum element of each row and each column in a matrix
- Print index of columns sorted by count of zeroes in the Given Matrix
- Count all the columns in a matrix which are sorted in descending
- Minimum operations required to make each row and column of matrix equals
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix
- Count zeros in a row wise and column wise sorted matrix
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Print all possible paths from the first row to the last row in a 2D array
- Count of ways to generate a Matrix with product of each row and column as 1 or -1
- Enlarge a Matrix such that each element occurs in R rows and C columns
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Find a common element in all rows of a given row-wise sorted matrix
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Search in a sorted 2D matrix (Stored in row major order)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.