Write a function to find the longest common prefix string amongst an array of strings.
class Solution {
public:
string longestCommonPrefix(vector<string> &strs)
{
int n = strs.size();
string ans = "";
if(n == 0)
return ans;
int m = strs[0].size();
if(m == 0)
return ans;
char c = strs[0][0];
int k = 0;
while(true)
{
for(int i=0; i<n; i++)
{
if(k > strs[i].size() || strs[i][k] != c)
return ans;
}
ans += c;
++k;
if(k < strs[0].size())
c = strs[0][k];
else
return ans;
}
return ans;
}
};