-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path122WordPattern.cpp
More file actions
44 lines (36 loc) · 1.08 KB
/
Copy path122WordPattern.cpp
File metadata and controls
44 lines (36 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
#include<vector>
#include<unordered_map>
#include<sstream>
using namespace std;
class Solution {
public:
bool wordPattern(string pattern, string s) {
// split s into words
vector<string> words;
string w;
stringstream ss(s);
while (ss >> w) words.push_back(w);
if (words.size() != pattern.size()) return false;
unordered_map<char, string> c2w;
unordered_map<string, char> w2c;
for (int i = 0; i < (int)pattern.size(); i++) {
char c = pattern[i];
const string &word = words[i];
// if c already mapped, it must match current word
if (c2w.count(c) && c2w[c] != word) return false;
// if word already mapped, it must match current char
if (w2c.count(word) && w2c[word] != c) return false;
// set/confirm mapping
c2w[c] = word;
w2c[word] = c;
}
return true;
}
};
int main(){
Solution obj;
cout<<boolalpha;
cout<<obj.wordPattern("abba", "dog cat cat dog");
return 0;
}