题目来源
题目描述
题目解析
class Solution {
public:
int prefixCount(vector<string>& words, string pref) {
int ans = 0, len_p = pref.size();
for(auto & word : words){
if(word.size() >= len_p){
bool flag = true;
for (int i = 0; i < len_p; ++i) {
if(word[i] != pref[i]){
flag = false;
break;
}
}
if(flag){
++ans;
}
}
}
return ans;
}
};
class Solution {
public:
int prefixCount(vector<string>& words, string pref) {
int sum = 0;
for(const string& word : words){
int m = pref.length();
if(word.length() < m || word.substr(0, m) != pref){
continue;
}else{
++sum;
}
}
return sum;
}
};