还是有一种情况没考虑到,以后还是在if else的时候注意写完整把。
class Solution {
public:
void func1(string word, vector<char>& item, vector<int>& count){
item.push_back(word[0]);
count.push_back(1);
for (int i=1;i<word.size();i++){
if (word[i]==word[i-1]){
count[count.size()-1] ++;
}
else{
item.push_back(word[i]);
count.push_back(1);
}
}
return;
}
bool judge(vector<char>& itemS, vector<int>& countS, vector<char>& itemW, vector<int>& countW){
if (itemS.size()!=itemW.size()){
return false;
}
for (int i=0;i<itemS.size();i++){
if (itemS[i]!= itemW[i]){
return false;
}
}
for (int i=0;i<countS.size();i++){
if (countS[i]<countW[i]){
return false;
}
else if (countS[i]==countW[i]){
continue;
}
else{ //countS[i]>countW[i] //||
if (countS[i]>=3){
continue;
}
else{
return false;
}
}
}
return true;
}
int expressiveWords(string S, vector<string>& words) {
vector<char> itemS;
vector<int> countS;
func1(S, itemS, countS);
/*
for (int i=0;i<itemS.size();i++){
cout<<itemS[i]<<" ";
}
cout<<endl;
for (int i=0;i<countS.size();i++){
cout<<countS[i]<<" ";
}*/
int res = 0;
for (int i=0;i<words.size();i++){
if (S.size()<words[i].size()){
continue;
}
vector<char> itemW;
vector<int> countW;
func1(words[i], itemW, countW);
if (judge(itemS, countS, itemW, countW)){
res ++;
}
}
return res;
}
};