Write a function to find the longest common prefix string amongst an array of strings.
Solution:
Code:
<span style="font-size:14px;">class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
const int lengthV = strs.size();
if (lengthV == 0) return "";
if (lengthV == 1) return strs[0];
bool dismatch;
const int length = strs[0].size();
for (int i = 0; i < length; ++i) {
dismatch = false;
for (int j = 1; j < lengthV; ++j)
if (strs[j][i] != strs[0][i]) {
dismatch = true;
break;
}
if (dismatch)
return strs[0].substr(0, i);
}
return strs[0];
}
};</span>