其他分享
首页 > 其他分享> > leetcode 5978. 统计追加字母可以获得的单词数

leetcode 5978. 统计追加字母可以获得的单词数

作者:互联网

 1 class Solution {
 2 public:
 3     int wordCount(vector<string>& startWords, vector<string>& targetWords) {
 4         int n=startWords.size(),ans=0;
 5         unordered_set<int>mp;
 6         for(auto &p:startWords)
 7         {
 8             int mask=0;
 9             for(auto &ch:p)mask|=(1<<(ch-'a'));
10             mp.insert(mask);
11         }
12         for(auto &p:targetWords)
13         {
14             int mask=0;
15             for(auto &ch:p)mask|=(1<<(ch-'a'));
16             for(auto &ch:p)
17             {
18                 if(mp.find(mask^(1<<(ch-'a')))!=mp.end())
19                 {
20                     ans++;
21                     break;
22                 }
23             }
24         }
25         return ans;
26     }
27 };

 

标签:vector,int,auto,ch,mask,startWords,单词,5978,leetcode
来源: https://www.cnblogs.com/matt-su/p/15780829.html