Advertisement
vaibhav1906

Match specific pattern

Apr 3rd, 2022
1,120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. vector<string> findMatchedWords(vector<string> dict,string pattern)
  2. {
  3.        //Your code here
  4.        unordered_map<char,int> m;
  5.        vector<string> ans;
  6.        int count = 1;
  7.        string p = "";
  8.        
  9.        for(char c : pattern){
  10.            if(m.find(c)==m.end()){
  11.                m.insert({c,count});
  12.                count++;
  13.            }
  14.            
  15.            p += to_string(m.find(c)->second);
  16.        }
  17.        
  18.        
  19.        for(string d : dict){
  20.            
  21.            unordered_map<char,int> mp;
  22.        
  23.            count = 1;
  24.            string t = "";
  25.            
  26.            for(char c : d){
  27.                if(mp.find(c)==mp.end()){
  28.                    mp.insert({c,count});
  29.                    count++;
  30.                }
  31.                
  32.                t += to_string(mp.find(c)->second);
  33.            }
  34.            
  35.            if(t==p)ans.push_back(d);
  36.            
  37.        }
  38.        
  39.        return ans;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement