-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
44 lines (36 loc) · 976 Bytes
/
main.cpp
File metadata and controls
44 lines (36 loc) · 976 Bytes
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 <algorithm>
#include <string>
#include <unordered_map>
#include <sstream>
bool wordPattern(std::string pattern, std::string str) {
std::vector<std::string> words;
std::string word;
std::istringstream iss(str, std::istringstream::in);
while (iss >> word) {
words.push_back(word);
}
if (pattern.length() != words.size()) {
return false;
}
std::unordered_map<char, std::string> mc;
std::unordered_map<std::string, char> mw;
for (int i = 0; i < pattern.size(); i++) {
char c = pattern[i];
std::string w = words[i];
if (!mc.count(c) && !mw.count(w)) {
mc[c] = w;
mw[w] = c;
} else {
if (mc[c] != w) {
return false;
}
}
}
return true;
}
int main() {
std::cout << wordPattern("aa", "abba abba") << std::endl;
return 0;
}