Write a function to find the longest common prefix string amongst an array of strings.
Solution:
class Solution
{
public:
string longestCommonPrefix(vector<string> &strs)
{
vector<string> s = strs;
string temp="";
int n = s.size();
if(n <= 0)
return temp;
temp = s[0];
int i=1;
while(i<n)
{
temp = longest(s[i],temp);
i++;
}
return temp;
}
string longest(const string& s, const string& t)
{
string res="";
int m = s.length();
int n = t.length();
n = min(m,n);
int i=0;
while(i<n)
{
if(s[i] == t[i])
res += s[i];
else
return res;
i++;
}
return res;
}
int min(int i, int j)
{
return i>j? j:i;
}
};