Leetcode-890 Find and Replace Pattern(查找和替换模式)

 1 class Solution
 2 {
 3     public:
 4         vector<string> findAndReplacePattern(vector<string>& words, string pattern)
 5         {
 6             vector<string> result;
 7             map<int,int> store;
 8             map<int,int> store2;
 9 
10             for(int i = 0; i < words.size(); i ++)
11             {
12                 int pattern_index = 0;
13                 int flag = 0;
14                 store.clear();
15                 store2.clear();
16                 for(auto c:words[i])
17                 {
18                     if(store.find(c)==store.end())
19                     {
20                         if(store2.find(pattern[pattern_index])==store2.end())
21                         {
22                             store.insert(make_pair(c,pattern[pattern_index]));
23                             store2.insert(make_pair(pattern[pattern_index],c));
24                         }
25                         else
26                         {
27                             flag = 1;
28                             break;
29                         }
30                     }
31                     else
32                     {
33                         if(store.find(c)->second!=pattern[pattern_index])
34                         {
35                             flag = 1;
36                             break;
37                         }
38                     }
39                     pattern_index ++;
40                 }
41                 if(flag==0)
42                 {
43                     result.emplace_back(words[i]);
44                 }
45             }
46             return result;
47         }
48 };

 

posted @ 2018-08-19 11:04  Asurudo  阅读(418)  评论(0编辑  收藏  举报