<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>

struct Node{
    Node *links[26];
    bool end=false;
    void setNext(char ch,Node *node)
    {
        links[ch-'a']=node;
    }
    Node *getNext(char ch)
    {
        return links[ch-'a'];
    }
    bool containsKey(char ch)
    {
        return links[ch-'a']!=NULL;
    }
    void setEnd()
    {
        end=true;
    }
    bool isEnd()
    {
        return end;
    }
};
bool cmp(string &a,string &b)
{
    return a.size()<b.size();
}
bool find(Node *node,string &word)
{

    for(int i=0;i<word.size();i++)
    {
        if(!node->containsKey(word[i]))
            return false;
        node=node->getNext(word[i]);
    }
    return node->isEnd();
}
class Solution {
public:
    int longestStrChain(vector<string>& words) {
        int n=words.size();
        Node *root=new Node();
        sort(words.begin(),words.end(),cmp);
        // map<string,int> mp;
        vector<pair<string,int>> vec;
        set<string> mp;
        map<string,int> mp2;
        // vector<int> dp;
        int c=0;
        for(int i=0;i<n;i++)
        {
            Node *node=root;
            for(int j=0;j<words[i].size();j++)
            {
                if(!node->containsKey(words[i][j]))
                {
                    node->setNext(words[i][j],new Node());
                }
                node=node->getNext(words[i][j]);
            }
            node->setEnd();
            if(mp.find(words[i])==mp.end())
            {
                // mp[words[i]]=c;
                mp.insert(words[i]);
                vec.push_back({words[i],c});
                mp2[words[i]]=c;
                c++;
            }
        }
        
        vector<int> dp(mp.size(),1);
        for(auto it:vec)
        {
            // cout<<it.first<<" "<<dp[it.second]<<endl;
            for(int j=97;j<=122;j++)
            {
                string s;
                s.push_back(char(j));
                for(int k=0;k<it.first.size();k++)
                    s.push_back(it.first[k]);
                Node *node=root;
                if(mp.find(s)!=mp.end())
                {
                    // cout<<s<<" "<<i<<endl;
                    dp[mp2[s]]=max(dp[mp2[s]],dp[it.second]+1);
                }
                
            }
            for(int k2=97;k2<=122;k2++)
            {
            for(int j=0;j<it.first.size();j++)
            {
                string s;
                for(int k=0;k<=j;k++)
                    s.push_back(it.first[k]);
              
                    s.push_back(char(k2));
                for(int k=j+1;k<it.first.size();k++)
                    s.push_back(it.first[k]);
                // cout<<s;
                Node *node=root;
                if(mp.find(s)!=mp.end())
                {
                    dp[mp2[s]]=max(dp[mp2[s]],dp[it.second]+1);
                }
            }
            }
        
}
        return *max_element(dp.begin(),dp.end());
        // return 0;
    }
};
392321