Write a function to find the longest common prefix string amongst an array of strings.
#define MAX_LEN 0xFFFF
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (strs.size() == 0)
{
return "";
}
if (strs.size() == 1)
{
return strs[0];
}
string commProfixStr = "";
int vectorSize = strs.size();
int maxSize = MAX_LEN;
for (int i = 0; i<vectorSize; i++)
{
if (strs[i].size() < maxSize)
{
maxSize = strs[i].size();
}
}
for (int i = 0; i<maxSize; i++)
{
char standChar = strs[0][i];
bool allMatch = true;
for (int j = 0; j<strs.size(); j++)
{
if (strs[j][i] != standChar)
{
allMatch = false;
break;
}
}
if (allMatch)
{
commProfixStr += standChar;
}
else
{
break;
}
}
return commProfixStr;
}
};