-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordPattern.cpp
30 lines (28 loc) · 962 Bytes
/
wordPattern.cpp
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
// https://leetcode.com/problems/word-pattern/
// 都映射为[1,2,2,1] 这样的形式。使用模板
class Solution {
public:
bool wordPattern(string pattern, string str) {
stringstream ss(str);
istream_iterator<string> in_iter(ss), eof;
vector<string> vecStr(in_iter, eof);
auto res1 = mapToOrdinal(pattern.begin(), pattern.end());
auto res2= mapToOrdinal(vecStr.begin(), vecStr.end());
return res1 == res2;
}
// map [abba] to [1,2,2,1]
template<typename Iter>
vector<int> mapToOrdinal(Iter beg, Iter end) {
int i=0;
vector<int> res;
map<typename std::remove_reference<decltype(*beg)>::type, int> mapping;
for (auto it=beg; it!=end; ++it) {
auto found = mapping.find(*it);
if (found == mapping.end()) {
mapping[*it] = ++i;
}
res.push_back(mapping[*it]);
}
return res;
}
};