Advertisement
nikunjsoni

890

May 21st, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
  4.         char m1[26], m2[26];
  5.         vector<string> res;
  6.         int i;
  7.         for(auto word: words){
  8.             for(i=0; i<26; i++)
  9.                 m1[i] = m2[i] = -1;
  10.             for(i=0; i<word.size(); i++){
  11.                 int ch1 = word[i]-'a', ch2 = pattern[i]-'a';
  12.                 if(m1[ch1] == -1 && m2[ch2] == -1){
  13.                     m1[ch1] = ch2;
  14.                     m2[ch2] = ch1;
  15.                 }
  16.                 else
  17.                     if(m1[ch1] != ch2 || m2[ch2] != ch1)
  18.                         break;
  19.             }
  20.             if(i == word.size()) res.push_back(word);
  21.         }
  22.         return res;
  23.     }
  24. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement