Formatted question description: https://leetcode.ca/all/1048.html

1048. Longest String Chain (Medium)

Given a list of words, each word consists of English lowercase letters.

Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac".

A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on.

Return the longest possible length of a word chain with words chosen from the given list of words.

 

Example 1:

Input: ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: one of the longest word chain is "a","ba","bda","bdca".

 

Note:

  1. 1 <= words.length <= 1000
  2. 1 <= words[i].length <= 16
  3. words[i] only consists of English lowercase letters.

 

Related Topics:
Hash Table, Dynamic Programming

Solution 1.

// OJ: https://leetcode.com/problems/longest-string-chain/
// Time: O(N^2)
// Space: O(N)
class Solution {
public:
    int longestStrChain(vector<string>& A) {
        int N = A.size(), ans = 1;
        vector<int> id(N), len(N, 1);
        iota(begin(id), end(id), 0);
        sort(begin(id), end(id), [&](int a, int b) { return A[a].size() < A[b].size(); });
        vector<array<int, 26>> cnts(N);
        map<int, vector<int>> m;
        for (int i = 0; i < N; ++i) {
            for (char c : A[i]) cnts[i][c - 'a']++;
            m[A[i].size()].push_back(i);
        }
        for (auto &[cnt, ids] : m) {
            if (m.count(cnt - 1) == 0) continue;
            for (int i : ids) {
                for (int j : m[cnt - 1]) {
                    int diff = 0;
                    for (int k = 0; k < 26; ++k) {
                        diff += abs(cnts[i][k] - cnts[j][k]);
                        if (diff > 1) break;
                    }
                    if (diff == 1) len[i] = max(len[i], len[j] + 1);
                }
                ans = max(ans, len[i]);
            }
        }
        return ans;
    }
};

Solution 2.

// OJ: https://leetcode.com/problems/longest-string-chain/
// Time: O(NSS)
// Space: O(NS)
// Ref: https://leetcode.com/problems/longest-string-chain/discuss/294890/C%2B%2BJavaPython-DP-Solution
class Solution {
public:
    int longestStrChain(vector<string>& A) {
        auto cmp = [](string &a, string &b) { return a.size() < b.size(); };
        sort(begin(A), end(A), cmp);
        unordered_map<string, int> dp;
        int ans = 1;
        for (auto &s : A) {
            for (int i = 0; i < s.size(); ++i) {
                dp[s] = max(dp[s], dp[s.substr(0, i) + s.substr(i + 1)] + 1);
                ans = max(ans, dp[s]);
            }
        }
        return ans;
    }
};

Java

  • class Solution {
        public int longestStrChain(String[] words) {
            Arrays.sort(words, new Comparator<String>() {
                public int compare(String str1, String str2) {
                    if (str1.length() != str2.length())
                        return str1.length() - str2.length();
                    else
                        return str1.compareTo(str2);
                }
            });
            int smallestLength = words[0].length();
            int nextLength = smallestLength + 1;
            int length = words.length;
            int[] dp = new int[length];
            int dpStartIndex = -1;
            for (int i = 0; i < length; i++) {
                dp[i] = 1;
                if (words[i].length() == nextLength && dpStartIndex < 0)
                    dpStartIndex = i;
            }
            if (dpStartIndex < 0)
                return 1;
            for (int i = dpStartIndex; i < length; i++) {
                String curWord = words[i];
                for (int j = i - 1; j >= 0; j--) {
                    String prevWord = words[j];
                    if (prevWord.length() == curWord.length())
                        continue;
                    else if (prevWord.length() < curWord.length() - 1)
                        break;
                    else {
                        if (isSubsequence(prevWord, curWord))
                            dp[i] = Math.max(dp[i], dp[j] + 1);
                    }
                }
            }
            int maxLength = 0;
            for (int num : dp)
                maxLength = Math.max(maxLength, num);
            return maxLength;
        }
    
        public boolean isSubsequence(String str1, String str2) {
            int length1 = str1.length(), length2 = str2.length();
            if (length1 > length2)
                return false;
            int index1 = 0, index2 = 0;
            while (index1 < length1 && index2 < length2) {
                char c1 = str1.charAt(index1), c2 = str2.charAt(index2);
                if (c1 == c2)
                    index1++;
                index2++;
            }
            return index1 == length1;
        }
    }
    
  • // OJ: https://leetcode.com/problems/longest-string-chain/
    // Time: O(N * S^2)
    // Space: O(NS)
    class Solution {
        unordered_map<string, int> m; 
        int dp(const string &s) {
            if (m[s]) return m[s];
            if (s.size() == 1) return 1;
            int ans = 1;
            for (int i = 0; i < s.size(); ++i) {
                auto copy = s;
                copy.erase(begin(copy) + i);
                if (m.count(copy)) {
                    ans = max(ans, 1 + dp(copy));
                }
            }
            return m[s] = ans;
        }
    public:
        int longestStrChain(vector<string>& A) {
            for (auto &s : A) m[s] = 0;
            int ans = 0;
            for (const auto &[s, len] : m) {
                ans = max(ans, dp(s));
            }
            return ans;
        }
    };
    
  • # 1048. Longest String Chain
    # https://leetcode.com/problems/longest-string-chain/
    
    class Solution:
        def longestStrChain(self, words: List[str]) -> int:
            dp = {}
            res = 1
            
            for word in sorted(words, key = len):
                dp[word] = 1
                
                for i in range(len(word)):
                    prev = word[:i] + word[i + 1:]
                    
                    if prev in dp:
                        dp[word] = max(dp[word], dp[prev] + 1)
                        res = max(res, dp[word])
            
            return res
    
    

All Problems

All Solutions