Write a function to find the longest common prefix string amongst an array of strings.
class Solution {
public:
string longestCommonPrefix(vector<string>& strs) {
int len = strs.size();
if(len == 1)
return strs[0];
else if(len == 0)
return "";
int n = 0;
sort(strs.begin(), strs.end());
bool flag = true;
while(flag && n < strs[0].length())
{
int i;
for(i=1; i<len; i++)
{
if(strs[0][n] != strs[i][n])
{
flag = false;
break;
}
}
if(i == len)
n++;
}
string pre = strs[0].substr(0, n);
return pre;
}
};