<p>You are given an array of <code>words</code> where each word consists of lowercase English letters.</p>

<p><code>word<sub>A</sub></code> is a <strong>predecessor</strong> of <code>word<sub>B</sub></code> if and only if we can insert <strong>exactly one</strong> letter anywhere in <code>word<sub>A</sub></code> <strong>without changing the order of the other characters</strong> to make it equal to <code>word<sub>B</sub></code>.</p>

<ul>
	<li>For example, <code>&quot;abc&quot;</code> is a <strong>predecessor</strong> of <code>&quot;ab<u>a</u>c&quot;</code>, while <code>&quot;cba&quot;</code> is not a <strong>predecessor</strong> of <code>&quot;bcad&quot;</code>.</li>
</ul>

<p>A <strong>word chain</strong><em> </em>is a sequence of words <code>[word<sub>1</sub>, word<sub>2</sub>, ..., word<sub>k</sub>]</code> with <code>k &gt;= 1</code>, where <code>word<sub>1</sub></code> is a <strong>predecessor</strong> of <code>word<sub>2</sub></code>, <code>word<sub>2</sub></code> is a <strong>predecessor</strong> of <code>word<sub>3</sub></code>, and so on. A single word is trivially a <strong>word chain</strong> with <code>k == 1</code>.</p>

<p>Return <em>the <strong>length</strong> of the <strong>longest possible word chain</strong> with words chosen from the given list of </em><code>words</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;a&quot;,&quot;b&quot;,&quot;ba&quot;,&quot;bca&quot;,&quot;bda&quot;,&quot;bdca&quot;]
<strong>Output:</strong> 4
<strong>Explanation</strong>: One of the longest word chains is [&quot;a&quot;,&quot;<u>b</u>a&quot;,&quot;b<u>d</u>a&quot;,&quot;bd<u>c</u>a&quot;].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;xbc&quot;,&quot;pcxbcf&quot;,&quot;xb&quot;,&quot;cxbc&quot;,&quot;pcxbc&quot;]
<strong>Output:</strong> 5
<strong>Explanation:</strong> All the words can be put in a word chain [&quot;xb&quot;, &quot;xb<u>c</u>&quot;, &quot;<u>c</u>xbc&quot;, &quot;<u>p</u>cxbc&quot;, &quot;pcxbc<u>f</u>&quot;].
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> words = [&quot;abcd&quot;,&quot;dbqca&quot;]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The trivial word chain [&quot;abcd&quot;] is one of the longest word chains.
[&quot;abcd&quot;,&quot;dbqca&quot;] is not a valid word chain because the ordering of the letters is changed.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= words.length &lt;= 1000</code></li>
	<li><code>1 &lt;= words[i].length &lt;= 16</code></li>
	<li><code>words[i]</code> only consists of lowercase English letters.</li>
</ul>

class Solution {
public:

bool helper2(int i , int j, string text1 , string text2 , vector<vector<int>> &dp1){
    if(j == text2.size()){
        return true;
    }
    if(i == text1.size()){
        return false;
    }

    if(dp1[i][j] != -1){
        return dp1[i][j];
    }

    int inc = false;
    if(text1[i] == text2[j]){
        inc = helper2(i+1 , j+1 , text1 , text2 , dp1);
    }
    
    int exc = helper2(i+1 , j , text1 , text2 , dp1);

    return dp1[i][j] = inc || exc;
    
}

bool isSubseq(string text1 , string text2){
    int n = text1.size();
    int m = text2.size();
    
    vector<vector<int>> dp1(n+1 , vector<int>(m+1, -1));

    return helper2(0,0,text1,text2,dp1);
}

int helper(int i , int prev , vector<string> words , vector<vector<int>> &dp){
    if(i == words.size()){
        return 0;
    }

    if(dp[i][prev + 1] != -1){
        return dp[i][prev+1];
    }

    int inc = 0;
    if(prev == -1){
        inc = 1 + helper(i+1 , i , words , dp);
    }
    else if(words[i].size()-1 == words[prev].size() && isSubseq(words[i] , words[prev]) == true){
        inc = 1 + helper(i+1 , i , words , dp);
    }

    int exc = helper(i+1 , prev , words , dp);

    return dp[i][prev+1] = max(inc,exc);
   
}

static bool compareByLength(const std::string &a, const std::string &b) {
    return a.size() < b.size();
}

bool tabular_subseq(string text1 , string text2){
    int n = text1.size();
    int m = text2.size();
    
    vector<vector<bool>> dp1(n+1 , vector<bool>(m+1, false));

    for(int i=0; i<=n; i++){
        dp1[i][m] = true;
    }

    for(int i = n-1; i>=0; i--){
        for(int j=m-1; j>=0; j--){
            int inc = false;
            if(text1[i] == text2[j]){
                inc = dp1[i+1][j+1];
            }

            int exc = dp1[i+1][j];

            dp1[i][j] = inc || exc;
        }
    }

    return dp1[0][0];

}

int tabular(vector<string>& words){
    int n = words.size();

    vector<vector<int>> dp(n+1 , vector<int>(n+1 , 0));

    for(int i = n-1; i>=0; i--){
        for(int prev = n-1; prev >= -1; prev--){
            int inc = 0;
            if(prev == -1){
                inc = 1 + dp[i+1][i+1];
            }
            else if(words[i].size()-1 == words[prev].size() && tabular_subseq(words[i] , words[prev]) == true){
                int new_prev = i;
                inc = 1 + dp[i+1][new_prev + 1];
            }

            int exc = dp[i+1][prev+1];

            dp[i][prev + 1] = max(inc , exc);
        }
    }

    return dp[0][0];
    
}
    int longestStrChain(vector<string>& words) {
        int n = words.size();

        //vector<vector<int>> dp(n+1 , vector<int>(n+2 , -1));

        sort(words.begin(), words.end(), compareByLength);

        //return helper(0,-1,words,dp);
        return tabular(words);
    }
};
495233