VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-columns-to-be-deleted-to-make-each-row-sorted/

⇱ Count columns to be deleted to make each row sorted - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count columns to be deleted to make each row sorted

Last Updated : 16 Mar, 2023

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 1: 

Convert the given Vector/array of string into a char grid of n*m as n will be the number of row (number of string in vector/array) and m will be there size of string/length.then we will formally traverse over all the row and column and check for sorted or not if they are then continue. if they are not then we will save its column number and at the end we will return number of column that need to remove in order to make all the rows lexicographically sorted. 


Output
1
0

Approach 2: 

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: 


Output
1
0
Comment
Article Tags: