2022-11-03 08:27:54 +00:00
|
|
|
#include "s0014_longest_common_prefix.hpp"
|
|
|
|
|
2022-11-30 10:20:36 +00:00
|
|
|
string S0014::longestCommonPrefix(vector<string>& strs) {
|
2022-11-03 08:27:54 +00:00
|
|
|
string o{""};
|
|
|
|
int size = strs.size();
|
|
|
|
int minLen = strs.at(0).length();
|
|
|
|
for (int i{0}; i < size; i++) {
|
|
|
|
if (strs.at(i).length() < minLen) {
|
|
|
|
minLen = strs.at(i).length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// string index
|
|
|
|
for (int i{0}; i < minLen; i++) {
|
|
|
|
string tmp = strs.at(0).substr(i, 1);
|
|
|
|
// vector index
|
|
|
|
for (int j{0}; j < size; j++) {
|
|
|
|
if (strs.at(j).substr(i, 1) == tmp) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
o.append(tmp);
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|