<p>Write a function to find the longest common prefix string amongst an array of strings.</p>

<p>If there is no common prefix, return an empty string <code>&quot;&quot;</code>.</p>

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

<pre>
<strong>Input:</strong> strs = [&quot;flower&quot;,&quot;flow&quot;,&quot;flight&quot;]
<strong>Output:</strong> &quot;fl&quot;
</pre>

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

<pre>
<strong>Input:</strong> strs = [&quot;dog&quot;,&quot;racecar&quot;,&quot;car&quot;]
<strong>Output:</strong> &quot;&quot;
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>

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

<ul>
	<li><code>1 &lt;= strs.length &lt;= 200</code></li>
	<li><code>0 &lt;= strs[i].length &lt;= 200</code></li>
	<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>

#include <bits/stdc++.h>

using namespace std;

static const int nSize = 26;
struct Node{
    vector<int> children;
    bool isLast;
    Node(){
        children = vector<int>(nSize, -1);
        isLast = false;
    }
};

struct Trie{
    vector<Node> mainTrie;
    string actualLongestPrefixTillNow="";
    Trie(){
        mainTrie.emplace_back();
    }
    void addWord(string& w, bool isfirst = false){
        int rootTillHere  = 0;
        string longestPrefixTillNow = "";

        for(auto &x: w){
            int index = x-'a';
            if(mainTrie[rootTillHere].children[index]==-1){
                if(longestPrefixTillNow.size()<actualLongestPrefixTillNow.size() && !isfirst){
                    actualLongestPrefixTillNow = longestPrefixTillNow;
                }
                
                mainTrie[rootTillHere].children[index] = mainTrie.size();
                mainTrie.emplace_back();
            }
            longestPrefixTillNow+=x;
            rootTillHere =  mainTrie[rootTillHere].children[index];
        }
        mainTrie[rootTillHere].isLast = true;
        if(longestPrefixTillNow.size()<actualLongestPrefixTillNow.size()){
                    actualLongestPrefixTillNow = longestPrefixTillNow;
        }
    }
};

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        Trie t = Trie();
        string ans = strs[0];
        t.actualLongestPrefixTillNow = ans;
        int i=0;
        for(auto &x: strs){
            if(i==0){
                t.addWord(x, true);
                i++;
                continue;
            }
            t.addWord(x);
        }
        return t.actualLongestPrefixTillNow;
    }
};
12600