Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 280420a

Browse files
committed
Prob 181: Word pattern problem
1 parent e1b6bcc commit 280420a

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

‎README.md‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 180 |
9+
| Total Problems | 181 |
1010

1111
</center>
1212

@@ -257,3 +257,8 @@ Include contains single header implementation of data structures and some algori
257257
| Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ) | [remove_invalid_parenthesis.cpp](leet_code_problems/remove_invalid_parenthesis.cpp)|
258258
| Given an array and a value, remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. The order of elements can be changed. It doesn't matter what you leave beyond the new length.| [remove_element.cpp](leet_code_problems/remove_element.cpp)|
259259
| Find intersection of two arrays/vectors, Given two vectors find the result of their interaction. The result should only contain unique characters and can be in any order|[intersection_of_array.cpp](leet_code_problems/intersection_of_array.cpp)|
260+
| Given a pattern and a string str, find if str 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 str. example:
261+
pattern = "abba", str = "dog cat cat dog" should return true.
262+
pattern = "abba", str = "dog cat cat fish" should return false.
263+
pattern = "aaaa", str = "dog cat cat dog" should return false.
264+
pattern = "abba", str = "dog dog dog dog" should return false.| [word_pattern.cpp](leet_code_problems/word_pattern.cpp)|

‎leet_code_problems/word_pattern.cpp‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
*
3+
* Given a pattern and a string str, find if str follows the same pattern.
4+
* Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
5+
* Examples:
6+
* pattern = "abba", str = "dog cat cat dog" should return true.
7+
* pattern = "abba", str = "dog cat cat fish" should return false.
8+
* pattern = "aaaa", str = "dog cat cat dog" should return false.
9+
* pattern = "abba", str = "dog dog dog dog" should return false.
10+
* Notes:
11+
* You may assume pattern contains only lowercase letters, and str
12+
* contains lowercase letters separated by a single space.
13+
*
14+
* Approach:
15+
*
16+
* Split the string into words, and then map it to each character of the patterns as _we move.
17+
* If the character is being mapped to multiple words, or a word is being mapped to multiple
18+
* character, then the str is not following pattern.
19+
*/
20+
21+
#include <iostream>
22+
#include <vector>
23+
#include <sstream>
24+
#include <unordered_map>
25+
#include <unordered_set>
26+
27+
bool match_pattern(const std::string& str, const std::string& pattern)
28+
{
29+
// split string to words
30+
std::istringstream iss(str);
31+
std::vector<std::string> words(std::istream_iterator<std::string>{iss},
32+
std::istream_iterator<std::string>());
33+
if (words.size() != pattern.size()) {
34+
return false;
35+
}
36+
37+
std::unordered_map<char, std::string> map;
38+
std::unordered_set<std::string> set;
39+
for (int i = 0; i < pattern.size(); ++i) {
40+
if (map.find(pattern[i]) != map.end()) {
41+
// pattern is already mapped to some other string.
42+
if (words[i] != map[pattern[i]]) {
43+
return false;
44+
}
45+
} else {
46+
// word is already mapped to some other string.
47+
if (set.count(words[i])) {
48+
return false;
49+
} else {
50+
map[pattern[i]] = words[i];
51+
set.insert(words[i]);
52+
}
53+
}
54+
}
55+
return true;
56+
}
57+
58+
int main() {
59+
std::string pattern{"abba"};
60+
std::string str{"dog cat cat dog"};
61+
std::cout << "Pattern : " << pattern << std::endl;
62+
std::cout << "String :" << str << std::endl;
63+
bool r = match_pattern(str, pattern);
64+
std::string result = (r) ? "true" : "false";
65+
std::cout << "Result :" << result << std::endl;
66+
67+
pattern = "aaaa";
68+
std::cout << "Pattern : " << pattern << std::endl;
69+
std::cout << "String :" << str << std::endl;
70+
r = match_pattern(str, pattern);
71+
result = (r) ? "true" : "false";
72+
std::cout << "Result :" << result << std::endl;
73+
74+
75+
return 0;
76+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /