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

If there is no common prefix, return an empty string "".

&nbsp;
Example 1:

Input: strs = ["flower","flow","flight"]
Output: "fl"


Example 2:

Input: strs = ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.


&nbsp;
Constraints:


	1 &lt;= strs.length &lt;= 200
	0 &lt;= strs[i].length &lt;= 200
	strs[i] consists of only lowercase English letters.


class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        cmp=strs[0]
        for i in range(1,len(strs)):
            l=0
            if (len(cmp)>len(strs[i])):
                l+=len(strs[i])
            else:
                l+=len(cmp)
            ans=""
            for j in range(l):
                if (cmp[j]!=strs[i][j]):
                    if (j==0):
                        return ""
                    else:
                        break
                else:
                    ans+=strs[i][j]
            cmp=ans
        return cmp
		
Upvote If you Like!!!
class TrieNode{
    TrieNode[] childs;
    int frequency;
    TrieNode(){
        childs = new TrieNode[26];
        this.frequency = 1;
    }
}

class Solution {

    TrieNode root = new TrieNode();

    public String longestCommonPrefix(String[] strs) {
        if(strs.length == 0) return "";
        if(strs.length == 1) return strs[0];
        for(String str : strs){
            insertIntoTrie(str.toLowerCase());
        }
        return findCommonPrefix(strs[0], strs.length);
    }

    private void insertIntoTrie(String str) {
        TrieNode ptr = root;
        for(int i=0; i<str.length(); i++){
            if(ptr.childs[str.charAt(i)-'a'] == null){
                ptr.childs[str.charAt(i)-'a'] = new TrieNode();
            } else {
                ptr.childs[str.charAt(i)-'a'].frequency++;
            }
            ptr = ptr.childs[str.charAt(i)-'a'];
        }
    }

    private String findCommonPrefix(String str, int n) {
        String ans = "";
        for(int i=0; i<str.length(); i++){
            if(root.childs[str.charAt(i) -'a'].frequency != n){
                return ans;
            }
            ans += str.charAt(i);
            root = root.childs[str.charAt(i)-'a'];
        }
        return ans;
    }
}
class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        //brute
        string ans="";
        string ref=strs[0];
        for(int i=0;i<ref.size();i++)
        {
            int j=1;
            for(;j<strs.size();j++)
            {
                if(ref[i]!=strs[j][i])
                    break;
            }
            if(j==strs.size())
                ans+=ref[i];
            else
                break;
        }
        return ans;
    }
};
 var longestCommonPrefix = function(strs) {
 let commonStr=strs[0];
 for(let i=1; i<strs.length;i++){
   let currentStr= strs[i]
    for(let j=0; j<commonStr.length;j++){
      if (commonStr[j]!==currentStr[j]){
          commonStr=currentStr.slice(0,j)  
          break;
      }
    }
}   
return commonStr 
Longest Common Prefix