class Solution {
public:
void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c){
std::string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while(std::string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2-pos1));
pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if(pos1 != s.length())
v.push_back(s.substr(pos1));
}
int isPrefixOfWord(string sentence, string searchWord) {
vector<string> t;
int pos = 0;
int len = searchWord.length();
SplitString(sentence, t, " ");
for (int i = 0; i < t.size(); i++) {
if (t[i].substr(0, len) == searchWord) {
return i + 1;
}
}
return -1;
}
};
class Solution {
public:
bool check(char a, string s) {
for (int j = 0; j < 5; j++)
if (a == s[j]) return true;
return false;
}
int maxVowels(string s, int k) {
string a = "aeiou";
int max = 0;
vector<int> n(s.length(), 0);
for (int i = 0; i < s.length(); i++) {
if (check(s[i], a)) {
n[i] = 1;
}
}
for (int i = 0; i < k; i++) {
if (n[i] == 1) max++;
}
int cnt = max;
for (int i = k ; i < s.length(); i++) {
if (check(s[i], a)) { //是元音
if (n[i - k] == 1) continue; //右移以后左边元音被移除
else {
cnt++; //左边不是元音
}
} else { //不是元音
if (n[i - k] == 1) cnt--; //左边元音被移出
}
max = max < cnt ? cnt : max;
}
return max;
}
};
原文:https://www.cnblogs.com/DengSchoo/p/12993819.html