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