Write a function to find the longest common prefix string amongst an array of strings.
Solution:
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
if(strs.size() == 0) return "";
int tail = strs[0].length()-1;
for(int i = 1; i < strs.size(); ++i)
{
tail = min(tail, (int)strs[i].length()-1);
int head = 0;
while(head <= tail && strs[i-1][head] == strs[i][head]) head++;
tail = head - 1;
if(tail < 0) return "";
}
return strs[0].substr(0, tail+1);
}
};