-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLongest String Chain_LC1048.cpp
48 lines (39 loc) · 1.55 KB
/
Longest String Chain_LC1048.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
// LeetCode 1048
// Bottom-Up Dynamic Programming; unordered_map and sort
// N:num. of words, L: max possible length of a word
// Time: O(N (logN + L^2) )
// Space: O(N) : umap
/* Steps:
- Sort the words by word's length.
- For each word, loop on all possible previous word with 1 letter missing.
- If previous word has seen, update the longest chain for the current word. */
class Solution {
public :
int longestStrChain(vector<string> &words) {
// key: word
// value: longest seq. possible with the key as the end
unordered_map<string, int> um;
// Sorting the list by word length.
std::sort(words.begin(), words.end(),
[](const std::string &w1, const std::string &w2) {
return w1.size() < w2.size(); // increasing order
});
int longest = 1;
for (const string &word : words) {
int currLen = 1;
int wsize = word.size();
// Find all possible predecessors for the current word
// by removing one letter at a time.
for (int i = 0; i < wsize; i++) {
// Delete the character at i'th pos
string prdcs = word.substr(0,i) + word.substr(i+ 1, wsize+1);
// Check whether um contains predecessor.
if (um.find(prdcs) != um.end())
currLen = max(currLen, um[prdcs]+1);
}
um[word] = currLen;
longest = max(longest, currLen);
}
return longest;
}
};