-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path290_Word Pattern.cpp
75 lines (61 loc) · 1.75 KB
/
290_Word Pattern.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// main.cpp
// 290_Word Pattern
//
// Created by HsuYu-wei on 2015/10/11.
// Copyright (c) 2015年 HsuYu-wei. All rights reserved.
//
#include <iostream>
#include <vector>
#include <string>
#include <locale>
#include <algorithm>
#include <map>
using namespace std;
bool wordPattern(string pattern, string str) {
string tmp = "";
string compare;
bool first = true;
int count = 0;
map<string, char> buffer;
for ( int i = 0; i <= str.size(); i++ ) {
char c = str[i];
if ( c == ' ' || i == str.size() ){
cout << tmp << endl;
if ( first ) {
compare += pattern[count];
buffer[tmp] = pattern[count];
first = false;
}
else {
if ( buffer.count(tmp) ) {
if ( pattern[count] != buffer[tmp] )
return false;
compare += buffer[tmp];
}
else {
map<string, char>::const_iterator it = buffer.begin();
while ( it != buffer.end() ) {
if ( it->second == pattern[count] )
return false;
++it;
}
buffer[tmp] = pattern[count];
compare += pattern[count];
}
}
tmp = "";
count++;
}
else
tmp += str[i];
}
cout << compare << endl;
return compare == pattern;
}
int main(int argc, const char * argv[]) {
string pattern = "adda";
string str = "cat dog dog cat";
cout << wordPattern(pattern, str) << endl;
return 0;
}