2020-03-17
1.题目描述
拼写单词
2.解析
直接进行搜索即可
3.代码
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int countCharacters(vector<string>& words, string chars) {
int l = words.size(),ll,i,j,k,cnt=0;
bool f[110];
for (i=0;i<l;i++){
memset(f,false,sizeof(f));
string tmp=words[i];
ll=tmp.length();
for (j=0;j<ll;j++){
for (k=0;k<chars.length();k++){
if (tmp[j]==chars[k]){
if (!f[k]) {
f[k]=true;
break;
}
}
}
if (k>=chars.length()) break;
}
if (j>=ll) cnt+=ll;
}
return cnt;
}
};
int main(){
Solution s;
vector<string> words={"cat","bt","hat","tree"};
string chars="atach";
cout<<s.countCharacters(words,chars)<<endl;
return 0;
}