Write a function to find the longest common prefix string amongst an array of strings.
Any ideas? Choose the first string as a prefix string, then compare it with others. choose the shortest same strings.
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
if(strs.size() <=0) return "";
string compare = strs[0];
for(int i = 1; i < strs.size(); i++){
int j = 0;
for(; j < min(compare.size(), strs[i].size()); j++){
if(compare[j]!= strs[i][j])
break;
}
compare = compare.substr(0, j);
}
return compare;
}
};