<p>A <strong>valid encoding</strong> of an array of <code>words</code> is any reference string <code>s</code> and array of indices <code>indices</code> such that:</p>

<ul>
	<li><code>words.length == indices.length</code></li>
	<li>The reference string <code>s</code> ends with the <code>&#39;#&#39;</code> character.</li>
	<li>For each index <code>indices[i]</code>, the <strong>substring</strong> of <code>s</code> starting from <code>indices[i]</code> and up to (but not including) the next <code>&#39;#&#39;</code> character is equal to <code>words[i]</code>.</li>
</ul>

<p>Given an array of <code>words</code>, return <em>the <strong>length of the shortest reference string</strong> </em><code>s</code><em> possible of any <strong>valid encoding</strong> of </em><code>words</code><em>.</em></p>

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

<pre>
<strong>Input:</strong> words = [&quot;time&quot;, &quot;me&quot;, &quot;bell&quot;]
<strong>Output:</strong> 10
<strong>Explanation:</strong> A valid encoding would be s = <code>&quot;time#bell#&quot; and indices = [0, 2, 5</code>].
words[0] = &quot;time&quot;, the substring of s starting from indices[0] = 0 to the next &#39;#&#39; is underlined in &quot;<u>time</u>#bell#&quot;
words[1] = &quot;me&quot;, the substring of s starting from indices[1] = 2 to the next &#39;#&#39; is underlined in &quot;ti<u>me</u>#bell#&quot;
words[2] = &quot;bell&quot;, the substring of s starting from indices[2] = 5 to the next &#39;#&#39; is underlined in &quot;time#<u>bell</u>#&quot;
</pre>

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

<pre>
<strong>Input:</strong> words = [&quot;t&quot;]
<strong>Output:</strong> 2
<strong>Explanation:</strong> A valid encoding would be s = &quot;t#&quot; and indices = [0].
</pre>

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

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

class Solution {
public:
    
    class Node{
        public:
        Node* links[26];
        
        Node(){
            for(int i=0; i<26; i++){
                links[i]=NULL;
            }
        }
    };
    
    class Trie{
         public:
        Node* root;
        
        Trie(){
            root= new Node();
        }
        
        void insert(string &word){
            Node* temp=root;
            
            for(int i=0; i<word.length(); i++){
                int idx=word[i]-'a';
                
                if(temp->links[idx]!=NULL){
                    temp=temp->links[idx];
                }
                else{
                    Node* newNode=new Node();
                    temp->links[idx]=newNode;
                    temp=newNode;
                }
            }
        }
        
        bool isPresent(string &word){
            Node* temp=root;
            
            for(int i=0; i<word.length(); i++){
                int idx=word[i]-'a';
                
                if(temp->links[idx]!=NULL){
                    temp=temp->links[idx];
                }
                else{
                    return false;
                }
            }
            
            return true;
        }
    };
    
    static bool cmp(string a, string b){
        if(a.length()>b.length()) return true;
        
        return false;
    }
    int minimumLengthEncoding(vector<string>& words) {
        
        sort(words.begin(), words.end(), cmp);
        
        
        for(int k=0; k<words.size(); k++){
            int i=0, j=words[k].length()-1;
            
            while(i<j){
                swap(words[k][i], words[k][j]);
                i++;
                j--;
            }
        }
        
        // for(auto i: words){
        //     cout<<i<<" ";
        // }
        
        Trie* trie=new Trie();
        
        int ans=0;
        
        for(int i=0; i<words.size(); i++){
            
            if(!trie->isPresent(words[i])){
                trie->insert(words[i]);
                int len=words[i].length()+1;
                ans+=len;
            }
        }
        return ans;
    }
};
65521