Leetcode-916 Word Subsets(单词子集)

 1 class Solution
 2 {
 3     public:
 4         vector<string> wordSubsets(vector<string>& A, vector<string>& B)
 5         {
 6             int hash[28];
 7             int hash2[28];
 8             memset(hash,0,sizeof(hash));
 9             for(auto s:B)
10             {
11                 memset(hash2,0,sizeof(hash2));
12                 for(auto c:s)
13                 {
14                     hash2[c-'a'] ++;
15                     if(hash2[c-'a'] > hash[c-'a'])
16                         hash[c-'a'] = hash2[c-'a'];
17                 }
18             }
19             
20             memset(hash2,0,sizeof(hash2));
21             vector<string> result;
22             int flag = 1;
23             for(auto s:A)
24             {
25                 flag = 1;
26                 memset(hash2,0,sizeof(hash2));
27                 for(auto c:s)
28                 {
29                     hash2[c-'a'] ++;
30                 }
31                 for(int i = 0 ;i < 28;i ++)
32                 {
33                     if(hash2[i] < hash[i])
34                     {
35                         flag = 0;
36                         break;
37                     }
38                 }
39                 if(flag)
40                     result.push_back(s);
41             }
42             return result;
43         }
44 };

 

posted @ 2018-09-30 11:52  Asurudo  阅读(480)  评论(0编辑  收藏  举报