hi, let Avik connect you.


** You can give your thought about this content and request modifications if needed from Ask For Modifications page. Thank You.

** You can check all the posts on the Posts page.

** More about me in the About Me section.

Delete Columns to Make Sorted


  • Approach:

    1. Traverse through each of the given string's ith position.

    2. Check if the ith character is lesser than the character at (i - 1)th position.

    3. If it is then increase the count by 1.

    4. Return the count.


  • Time and Space Complexity:

      • Time Complexity: O(MxN)

      • Space Complexity: O(1)


Code [C++]

class Solution {

public:

int minDeletionSize(vector<string>& strs) {

ios_base::sync_with_stdio(false);

cin.tie(NULL);

int colSize = strs[0].size(), rowSize = strs.size();

int cnt = 0;

for(int i=0; i<colSize; i++){

for(int j=1; j<rowSize; j++){

if(strs[j][i] < strs[j-1][i]){

cnt++; break;

}

}

}

return cnt;

}

};