-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongest String Chain.cpp
37 lines (37 loc) · 1.07 KB
/
Longest String Chain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
public:
bool checkPossible(string &s1, string &s2) {
if (s1.size() != s2.size() + 1) return false;
int first = 0;
int second = 0;
while (first < s1.size()) {
if (second < s2.size() && s1[first] == s2[second]) {
first++;
second++;
} else {
first++;
}
}
return second == s2.size();
}
static bool comp(const string &s1, const string &s2) {
return s1.size() < s2.size();
}
int longestStrChain(vector<string>& words) {
int n = words.size();
sort(words.begin(), words.end(), comp);
vector<int> dp(n+1, 1);
int maxi = 1;
for (int i = 1; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (checkPossible(words[i], words[j]) && 1 + dp[j] > dp[i]){
dp[i] = dp[j] + 1;
if (dp[i] > maxi) {
maxi = dp[i];
}
}
}
}
return maxi;
}
};