-
Notifications
You must be signed in to change notification settings - Fork 118
/
1048. Longest String Chain.cpp
68 lines (58 loc) · 2.4 KB
/
1048. 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//Runtime: 1240 ms, faster than 6.56% of C++ online submissions for Longest String Chain.
//Memory Usage: 325.5 MB, less than 100.00% of C++ online submissions for Longest String Chain.
class Solution {
public:
int longestStrChain(vector<string>& words) {
int N = words.size();
sort(words.begin(), words.end(), [](string& a, string& b){
return (a.size() == b.size()) ? (a < b) : (a.size() < b.size());
});
//at start, each word forms a chain of length 1
vector<int> length(N, 1);
int maxLen = 1;
//start from 2nd last word
for(int i = N-2; i >= 0; i--){
string word = words[i];
for(int j = i+1; j < N; j++){
string successor = words[j];
if(successor.size() == word.size()+1){
//check if word is a predecessor of successor
bool found = false;
for(int k = 0; k < successor.size(); k++){
if(successor.substr(0, k) + successor.substr(k+1) == word)
found = true;
}
if(found){
length[i] = max(length[i], length[j]+1);
}
}
}
maxLen = max(maxLen, length[i]);
}
return maxLen;
}
};
//https://leetcode.com/problems/longest-string-chain/discuss/294890/C%2B%2BJavaPython-DP-Solution
//Runtime: 152 ms, faster than 32.27% of C++ online submissions for Longest String Chain.
//Memory Usage: 49.9 MB, less than 100.00% of C++ online submissions for Longest String Chain.
//time: O(NlogN) for sorting
//time: O(NSS) for the for loop, S for the length of each word, S <= 16, second S for the generation of substring
//space: O(NS)
class Solution {
public:
int longestStrChain(vector<string>& words) {
int N = words.size();
unordered_map<string, int> length;
int ans = 1;
sort(words.begin(), words.end(), [](string& a, string& b){
return a.size() < b.size();
});
for(string& word : words){
for(int i = 0; i < word.size(); i++){
length[word] = max(length[word], length[word.substr(0, i)+word.substr(i+1)]+1);
}
ans = max(ans, length[word]);
}
return ans;
}
};