Given k strings, find the longest common prefix (LCP).
Example
For strings
"ABCD", "ABEF" and "ACEF", the LCP is "A"
For strings
"ABCDEFG", "ABCEFG" and "ABCEFA", the LCP is"ABC"
class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
int len = strs.size();
string res;
if(0 == len) return res;
int index = 0;
while(index<strs[0].length())
{
for(int i=1;i<len;i++)
{
if(strs[i][index] != strs[0][index])
{
return strs[0].substr(0, index);
}
}
index++;
}
return strs[0];
}
};