23- Longest String Chain
The problem can be found at the following link: Question Link
My Approach
Custom Comparison Function
cmp:Define a static bool function
cmpthat takes two string parameters,aandb.It compares two strings based on their lengths, returning
trueif the length ofais less than the length ofb, andfalseotherwise.
isSubsequenceFunction:Define a member function
isSubsequencethat takes two string parameters,sandt.Initialize an integer variable
jto 0. This variable will be used to iterate through the characters ofs.Iterate through the characters of string
tusing a for loop.Inside the loop, check if the current character in
tat indexiis equal to the current character insat indexj.If they are equal, increment the
jvariable.After the loop, check if the length of
sis equal toj. If it is, returntrue, indicating thatsis a subsequence oft. Otherwise, returnfalse.
longestStrChainFunction:Define a member function
longestStrChainthat takes a reference to a vector of strings,words.Sort the
wordsvector using the custom comparison functioncmp. This sorting is done in ascending order of string lengths.Initialize an integer variable
nto store the number of words in the input vectorwords.Create a dynamic programming (DP) vector
dpof sizen, where each element represents the length of the longest chain ending with the word at that index. Initialize all elements to 1 because each word forms a chain of length 1 by default.Initialize an integer variable
maxLento 1, which will be used to keep track of the maximum chain length found so far.
DP Loop:
Iterate through the
wordsvector using a nested for loop. The outer loop iterates fromi = 1ton-1, and the inner loop iterates fromj = 0toi-1.Inside the inner loop, check if
words[j]is a subsequence ofwords[i]and if the length ofwords[j]plus 1 is equal to the length ofwords[i]. This checks ifwords[i]can be part of a longer chain.If the conditions are met, update the
dp[i]value to be the maximum of its current value anddp[j] + 1. This means that we consider extending the chain from wordjto wordi.Update the
maxLenvariable with the maximum of its current value anddp[i].
Return Result:
After the DP loop, return the value stored in
maxLen, which represents the length of the longest string chain that can be formed using the input words.
Time and Auxiliary Space Complexity
Time Complexity:
O(n*n)Auxiliary Space Complexity:
O(n)
Code (C++)
class Solution {
public:
static bool cmp(string a, string b){
return a.length()<b.length();
}
bool isSubsequence(string s, string t) {
int j=0;
for(int i=0; i<t.length();i++){
if(t[i]==s[j]){
j++;
}
}
if(s.length()==j)return true;
return false;
}
int longestStrChain(vector<string>& words) {
sort(words.begin(),words.end(),cmp);
int n=words.size();
vector<int> dp(n, 1);
int maxLen = 1;
for(int i=1; i<n; i++){
for(int j=0; j<i; j++){
if(isSubsequence(words[j], words[i]) && words[j].length()+1==words[i].length()){
dp[i] = max(dp[i], dp[j]+1);
}
}
maxLen = max(maxLen, dp[i]);
}
return maxLen;
}
};
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the rishabhv12/Daily-Leetcode-Solution repository.
Last updated
Was this helpful?