-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword_pattern.cpp
More file actions
98 lines (82 loc) · 2.96 KB
/
word_pattern.cpp
File metadata and controls
98 lines (82 loc) · 2.96 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
290. Word Pattern
Given a pattern and a string s, find if s follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in
pattern and a non-empty word in s.
Time Complexity: O(n + m) where n is the length of pattern and m is the length of s
Space Complexity: O(w) where w is the number of unique words
*/
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <sstream>
using namespace std;
class Solution {
public:
bool wordPattern(string pattern, string s) {
vector<string> words = splitString(s);
if (pattern.length() != words.size()) {
return false;
}
unordered_map<char, string> pattern_to_word;
unordered_map<string, char> word_to_pattern;
for (int i = 0; i < pattern.length(); i++) {
char ch = pattern[i];
string word = words[i];
if (pattern_to_word.find(ch) != pattern_to_word.end()) {
if (pattern_to_word[ch] != word) {
return false;
}
} else {
pattern_to_word[ch] = word;
}
if (word_to_pattern.find(word) != word_to_pattern.end()) {
if (word_to_pattern[word] != ch) {
return false;
}
} else {
word_to_pattern[word] = ch;
}
}
return true;
}
private:
vector<string> splitString(const string& s) {
vector<string> words;
stringstream ss(s);
string word;
while (ss >> word) {
words.push_back(word);
}
return words;
}
};
// Test cases
int main() {
Solution solution;
// Example 1
string pattern1 = "abba";
string s1 = "dog cat cat dog";
cout << "Input: pattern = \"" << pattern1 << "\", s = \"" << s1 << "\"" << endl;
cout << "Output: " << (solution.wordPattern(pattern1, s1) ? "true" : "false") << endl; // Expected: true
cout << endl;
// Example 2
string pattern2 = "abba";
string s2 = "dog cat cat fish";
cout << "Input: pattern = \"" << pattern2 << "\", s = \"" << s2 << "\"" << endl;
cout << "Output: " << (solution.wordPattern(pattern2, s2) ? "true" : "false") << endl; // Expected: false
cout << endl;
// Example 3
string pattern3 = "aaaa";
string s3 = "dog cat cat dog";
cout << "Input: pattern = \"" << pattern3 << "\", s = \"" << s3 << "\"" << endl;
cout << "Output: " << (solution.wordPattern(pattern3, s3) ? "true" : "false") << endl; // Expected: false
cout << endl;
// Additional test case
string pattern4 = "abba";
string s4 = "dog dog dog dog";
cout << "Input: pattern = \"" << pattern4 << "\", s = \"" << s4 << "\"" << endl;
cout << "Output: " << (solution.wordPattern(pattern4, s4) ? "true" : "false") << endl; // Expected: false
return 0;
}