-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path290.word-pattern.cpp
More file actions
48 lines (47 loc) · 1000 Bytes
/
290.word-pattern.cpp
File metadata and controls
48 lines (47 loc) · 1000 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
45
46
47
48
/*
* @lc app=leetcode id=290 lang=cpp
*
* [290] Word Pattern
*/
#include <string>
#include <vector>
#include <sstream>
#include <map>
using namespace std;
// @lc code=start
class Solution
{
public:
bool wordPattern(string pattern, string s)
{
map<string, int> m;
vector<int> v;
int id = 0;
for (auto c : pattern)
{
string tmp_s = string(1, c);
if (m.find(tmp_s) == m.end())
{
m[tmp_s] = id++;
}
v.push_back(m[tmp_s]);
}
id = 0;
stringstream ss(s);
string tmp_s;
m.clear();
int idx = 0;
while (getline(ss, tmp_s, ' '))
{
if(idx >= v.size())
return false;
if (m.find(tmp_s) == m.end())
m[tmp_s] = id++;
if (m[tmp_s] != v[idx])
return false;
idx++;
}
return idx==v.size();
}
};
// @lc code=end