In the last post, I discussed how recognizing patterns is important in solving coding challenges and discussed the Longest Increasing Subsequence. I shall talk about applying the basics in this problem to solve one variation of LIS. Follow me here, on Twitter, LinkedIn or Github for more.
Problem
You are given an array of n strings strs, all of the same length.
We may choose any deletion indices, and we delete all the characters in those indices for each string.
For example, if we have strs = ["abcdef","uvwxyz"] and deletion indices {0, 2, 3}, then the final array after deletions is ["bef", "vyz"].
Suppose we chose a set of deletion indices answer such that after deletions, the final array has every string (row) in lexicographic order. (i.e., (strs[0][0] <= strs[0][1] <= ... <= strs[0][strs[0].length - 1]), and (strs[1][0] <= strs[1][1] <= ... <= strs[1][strs[1].length - 1]), and so on). Return the minimum possible value of answer.length.
Example 1:
Input: strs = ["babca","bbazb"]
Output: 3
Explanation: After deleting columns 0, 1, and 4, the final array is strs = ["bc", "az"].
Both these rows are individually in lexicographic order (ie. strs[0][0] <= strs[0][1] and strs[1][0] <= strs[1][1]).
Note that strs[0] > strs[1] - the array strs is not necessarily in lexicographic order.
Example 2:
Input: strs = ["edcba"]
Output: 4
Explanation: If we delete less than 4 columns, the only row will not be lexicographically sorted.
Example 3:
Input: strs = ["ghi","def","abc"]
Output: 0
Explanation: All rows are already lexicographically sorted.
Constraints:
n == strs.length
1 <= n <= 100
1 <= strs[i].length <= 100
strs[i] consists of lowercase English letters.
Discussion
We apply the same principle in LIS with the double for loops.The only difference is we check all the characters in one column are less than or equal to all the characters in the other. We do this as we are trying to maximize the number of columns to be deleted.
Solution
Java
class Solution {
public int minDeletionSize(String[] strs) {
int length=strs[0].length();
int [] dp=new int[length];
Arrays.fill(dp,1);
for(int col=1;col<length;col++){
for(int j=0;j<col;j++){
int numOfRows=0;
for(int i=0;i<strs.length;i++){
char curr=strs[i].charAt(col);
char prev=strs[i].charAt(j);
if(prev>curr) break;
else numOfRows++;
}
if(numOfRows==strs.length && dp[col]<=dp[j])dp[col]=dp[j]+1;
}
}
Arrays.sort(dp);
return length-dp[dp.length-1];
}
}
C#
public class Solution {
public int MinDeletionSize(string[] strs) {
int length=strs[0].Length;
int [] dp=new int[length];
Array.Fill(dp,1);
for(int col=1;col<length;col++){
for(int j=0;j<col;j++){
int numOfRows=0;
for(int i=0;i<strs.Length;i++){
char curr=strs[i][col];
char prev=strs[i][j];
if(prev>curr) break;
else numOfRows++;
}
if(numOfRows==strs.Length && dp[col]<=dp[j])dp[col]=dp[j]+1;
}
}
Array.Sort(dp);
return length-dp[dp.Length-1];
}
}
C++
class Solution {
public:
int minDeletionSize(vector<string>& strs) {
string first=strs[0];
int length=first.size();
vector<int> dp;
for(int i=0;i<length;i++)dp.push_back(1);
for(int col=1;col<length;col++){
for(int j=0;j<col;j++){
int numOfRows=0;
for(int i=0;i<strs.size();i++){
string str= strs[i];
char curr=str[col];
char prev=str[j];
if(prev>curr) break;
else numOfRows++;
}
if(numOfRows==strs.size() && dp[col]<=dp[j])dp[col]=dp[j]+1;
}
}
std::sort(dp.begin(),dp.end());
return length-dp[dp.size()-1];
}
};
Top comments (0)