knakul853

Untitled

Jun 13th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. /**
  2. knakul853
  3. **/
  4. class Solution {
  5. public:
  6.     bool wordPattern(string pattern, string str) {
  7.        
  8.        
  9.         unordered_map<string, int>w2i;
  10.         unordered_map<char, int>c2i;
  11.        
  12.         int n = (int)pattern.size();
  13.         int i=0;
  14.        
  15.         istringstream in(str);
  16.        
  17.         for(string word; in >> word; i++ )
  18.         {
  19.             if( i == n || w2i[word] != c2i[pattern[i]] )
  20.                 return false;
  21.            
  22.             w2i[word] = c2i[pattern[i]] = i+1;
  23.         }
  24.        
  25.         return i == n;
  26.        
  27.     }
  28. };
Add Comment
Please, Sign In to add comment