# Count columns to be deleted to make each row sorted

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

## Recommended Posts:

- Count all the columns in a matrix which are sorted in descending
- Print index of columns sorted by count of zeroes in the Given Matrix
- Count rows/columns with sum equals to diagonal sum
- Minimum number of deletions to make a sorted sequence
- Minimum increment or decrement operations required to make the array sorted
- Count all sorted rows in a matrix
- Count pairs from two sorted matrices with given sum
- Count of operations to make a binary string"ab" free
- Count elements smaller than or equal to x in a sorted matrix
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix
- Count zeros in a row wise and column wise sorted matrix
- Find the count of palindromic sub-string of a string in its sorted form
- 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
- Sum of columns of a 2-D Matrix where first element is odd

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.