You are given an array of words where each word consists of lowercase English letters.

wordA is a predecessor of wordB if and only if we can insert exactly one letter anywhere in wordA without changing the order of the other characters to make it equal to wordB.

  • For example, "abc" is a predecessor of "abac", while "cba" is not a predecessor of "bcad".

A word chain is a sequence of words [word1, word2, ..., wordk] with k >= 1, where word1 is a predecessor of word2, word2 is a predecessor of word3, and so on. A single word is trivially a word chain with k == 1.

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

Example 1:

Input: words = [“a”,“b”,“ba”,“bca”,“bda”,“bdca”]
Output: 4
Explanation: One of the longest word chains is [“a”,“ba”,“bda”,“bdca”].

Example 2:

Input: words = [“xbc”,“pcxbcf”,“xb”,“cxbc”,“pcxbc”]
Output: 5
Explanation: All the words can be put in a word chain [“xb”, “xbc”, “cxbc”, “pcxbc”, “pcxbcf”].

Example 3:

Input: words = [“abcd”,“dbqca”]
Output: 1
Explanation: The trivial word chain [“abcd”] is one of the longest word chains.
[“abcd”,“dbqca”] is not a valid word chain because the ordering of the letters is changed.

Constraints:

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

Approach - Bottom Up 1D DP

  • So basically we first sort array by string length
  • then for each pair we check 2 things first is there should be only 1 length difference and then we need to check if this is a predecessor or not
class Solution {
    public int longestStrChain(String[] words) {
        Arrays.sort(words, Comparator.comparingInt(String::length));
        int n = words.length, maxlen = 1;
        int[] dp = new int[n];
        Arrays.fill(dp,1);
        
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < i; j++) {
                if (words[i].length() == words[j].length()+1 && isValid(words[j], words[i]))
                    dp[i] = Math.max(dp[i], dp[j] + 1);
            }
            maxlen = Math.max(maxlen, dp[i]);
        }
        return maxlen;
    }
 
    private boolean isValid(String prev, String curr) {
        int i = 0, j = 0;
        while (i < prev.length() && j < curr.length()) {
            if (prev.charAt(i) == curr.charAt(j)) {
                i++;
                j++;
            } else {
                j++;
                if (j-i > 1)
                    return false;
            }
        }
        return true;
    }
}

Approach - Memoization

class Solution {
    Integer[][] dp;
    public int longestStrChain(String[] words) {
        Arrays.sort(words, Comparator.comparingInt(String::length));
        dp = new Integer[words.length][words.length];
        return dfs(words, -1, 0);
    }
 
    private int dfs(String[] s, int p, int c) {
        if (c == s.length)
            return 0;
 
        if (p != -1 && dp[p][c] != null)
            return dp[p][c];
 
        int take = 0, not;
        if (p == -1 || (s[c].length() == s[p].length()+1 && isValid(s[p], s[c])))
            take = 1 + dfs(s, c, c+1);
        not = dfs(s, p, c+1);
        if (p != -1)
            dp[p][c] = Math.max(take,not);
        return Math.max(take,not);
    }
 
    private boolean isValid(String prev, String curr) {
        if (curr.length() > prev.length() + 1)
            return false;
 
        int i = 0, j = 0;
        while (i < prev.length() && j < curr.length()) {
            if (prev.charAt(i) == curr.charAt(j)) {
                i++;
                j++;
            } else {
                j++;
                if (j - i > 1)
                    return false;
            }
        }
 
        return true;
    }
}

Approach - Recursion

  • First we need to sort the array based on length because of condition of finding the predecessor where the difference of length should only be 1 by definition
  • Then we have 2 things if the predecessor condition passed then we can add 1 otherwise we are just checking
class Solution {
    public int longestStrChain(String[] words) {
        Arrays.sort(words, Comparator.comparingInt(String::length));
        return dfs(words, -1, 0);
    }
 
    private int dfs(String[] s, int p, int c) {
        if (c == s.length)
            return 0;
 
        int take = 0, not;
        if (p == -1 || (s[c].length() == s[p].length()+1 && isValid(s[p], s[c])))
            take = 1 + dfs(s, c, c+1);
        not = dfs(s, p, c+1);
 
        return Math.max(take,not);
    }
 
    private boolean isValid(String prev, String curr) {
        if (curr.length() > prev.length() + 1)
            return false;
 
        int i = 0, j = 0;
        while (i < prev.length() && j < curr.length()) {
            if (prev.charAt(i) == curr.charAt(j)) {
                i++;
                j++;
            } else {
                j++;
                if (j - i > 1)
                    return false;
            }
        }
 
        return true;
    }
}