-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathleetcode290_word_pattern.cpp
85 lines (70 loc) · 1.98 KB
/
leetcode290_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
76
77
78
79
80
81
82
83
84
85
/**
* @file leetcode290_word_pattern.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2023/06/07 22:15
* @brief https://leetcode.com/problems/word-pattern
*
**/
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <sstream>
using namespace std;
class Solution {
public:
bool wordPattern(string pattern, string s) {
std::map<char, std::string> m;
std::set<std::string> used;
std::vector<std::string> words;
stringSplit(s, words);
size_t plen = pattern.length();
if (plen != words.size()) {
return false;
}
for (size_t i = 0; i < plen; ++i) {
char c = pattern[i];
auto iter = m.find(c);
if (iter == m.end()) {
if (used.find(words[i]) != used.end()) {
return false;
}
m.insert({c, words[i]});
used.insert(words[i]);
} else {
auto prev_word = iter->second;
if (prev_word != words[i]) {
return false;
}
}
}
return true;
}
private:
void stringSplit(std::string s, std::vector<std::string>& words) {
std::stringstream iss(s);
std::string word;
while (iss >> word) {
words.emplace_back(word);
}
}
};
int main() {
while (1) {
std::cout << "pattern string (Ctrl-C to exit): ";
std::string pattern;
if (!std::getline(std::cin, pattern)) {
return 0;
}
std::cout << "s string (Ctrl-C) to exit: ";
std::string s;
if (!std::getline(std::cin, s)) {
return 0;
}
Solution solution;
auto ret = solution.wordPattern(pattern, s);
std::cout << (ret ? "true" : "false") << std::endl;
}
return 0;
}