This is one of those problems where the clean idea matters more than the amount of code. For 1048. Longest String Chain, the solution in this repository is mainly a dynamic programming solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: dynamic programming.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are longestStrChain, length.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- Substring checks are convenient but not free, so they are part of the real complexity story.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- 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)
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 1240 ms, faster than 6.56% of C++ online submissions for Longest String Chain.
02//Memory Usage: 325.5 MB, less than 100.00% of C++ online submissions for Longest String Chain.
03class Solution {
04public:
05 int longestStrChain(vector<string>& words) {
06 int N = words.size();
07
08 sort(words.begin(), words.end(), [](string& a, string& b){
09 return (a.size() == b.size()) ? (a < b) : (a.size() < b.size());
10 });
11
12 //at start, each word forms a chain of length 1
13 vector<int> length(N, 1);
14 int maxLen = 1;
15
16 //start from 2nd last word
17 for(int i = N-2; i >= 0; i--){
18 string word = words[i];
19
20 for(int j = i+1; j < N; j++){
21 string successor = words[j];
22 if(successor.size() == word.size()+1){
23 //check if word is a predecessor of successor
24 bool found = false;
25 for(int k = 0; k < successor.size(); k++){
26 if(successor.substr(0, k) + successor.substr(k+1) == word)
27 found = true;
28 }
29 if(found){
30 length[i] = max(length[i], length[j]+1);
31 }
32 }
33 }
34
35 maxLen = max(maxLen, length[i]);
36 }
37
38 return maxLen;
39 }
40};
41
42//https://leetcode.com/problems/longest-string-chain/discuss/294890/C%2B%2BJavaPython-DP-Solution
43//Runtime: 152 ms, faster than 32.27% of C++ online submissions for Longest String Chain.
44//Memory Usage: 49.9 MB, less than 100.00% of C++ online submissions for Longest String Chain.
45//time: O(NlogN) for sorting
46//time: O(NSS) for the for loop, S for the length of each word, S <= 16, second S for the generation of substring
47//space: O(NS)
48class Solution {
49public:
50 int longestStrChain(vector<string>& words) {
51 int N = words.size();
52 unordered_map<string, int> length;
53 int ans = 1;
54
55 sort(words.begin(), words.end(), [](string& a, string& b){
56 return a.size() < b.size();
57 });
58
59 for(string& word : words){
60 for(int i = 0; i < word.size(); i++){
61 length[word] = max(length[word], length[word.substr(0, i)+word.substr(i+1)]+1);
62 }
63 ans = max(ans, length[word]);
64 }
65
66 return ans;
67 }
68};
Cost