# LeetCode1178. 猜字谜

words = [“aaaa”,“asas”,“able”,“ability”,“actt”,“actor”,“access”],
puzzles = [“aboveyz”,“abrodyz”,“abslute”,“absoryz”,“actresz”,“gaswxyz”]

1 个单词可以作为 "aboveyz" 的谜底 : "aaaa"
1 个单词可以作为 "abrodyz" 的谜底 : "aaaa"
3 个单词可以作为 "abslute" 的谜底 : "aaaa", "asas", "able"
2 个单词可以作为 "absoryz" 的谜底 : "aaaa", "asas"
4 个单词可以作为 "actresz" 的谜底 : "aaaa", "asas", "actt", "access"


class Solution {
public:
vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles) {
vector<int> cnt;
int len_w = words.size();
int len_p = puzzles.size();
map<int,int>mp;
map<int,int>::iterator it;
//排序后去除重复字符串
for(int j=0; j<len_w; j++) {
sort(words[j].begin(), words[j].end());  //先对字符串排序；
string::iterator itend = unique(words[j].begin(),words[j].end());
//返回出现重复元素的首地址；
words[j].erase(itend, words[j].end());//删除重复元素；
if(words[j].size()<=7) {
int data=0;
for(int k=0; k<words[j].size(); k++) {
data+=pow(2,words[j][k]-'a');
}
mp[data]++;
}
}
for(int j=0; j<len_p; j++) {
int data=0;
int count=0;
int first_char = puzzles[j][0]-'a';
for(int k=0; k<puzzles[j].size(); k++) {
data+=pow(2,puzzles[j][k]-'a');
}
data-=pow(2,first_char);
int substr=data;
while(data>0) {
int x=data+pow(2,first_char);
if(mp[x]>0) {
count+=mp[x];
}
data=(data-1) & substr;
if(data == 0){
x=data+pow(2,first_char);
if(mp[x]>0) {
count+=mp[x];
}
}
}
cnt.push_back(count);
}
return cnt;
}
};


09-06 284

09-05 1005
09-02 440
02-26 6