Write a function to find the longest common prefix string amongst an array of strings.
» Solve this problem
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 "";
}
int length = strs[0].size(), j = 0;
for (int i = 1; i < strs.size(); i++) {
for (j = 0; j < strs[i].size(); j++) {
if (j >= length || strs[i][j] != strs[0][j]) {
break;
}
}
length = j;
}
return strs[0].substr(0, length);
}
};