Skip to content

Commit

Permalink
Prob 181: Word pattern problem
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Jan 22, 2018
1 parent e1b6bcc commit 280420a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

| Current Status| Stats |
| :------------: | :----------: |
| Total Problems | 180 |
| Total Problems | 181 |

</center>

Expand Down Expand Up @@ -257,3 +257,8 @@ Include contains single header implementation of data structures and some algori
| 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)|
| 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)|
| 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)|
| 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:
pattern = "abba", str = "dog cat cat dog" should return true.
pattern = "abba", str = "dog cat cat fish" should return false.
pattern = "aaaa", str = "dog cat cat dog" should return false.
pattern = "abba", str = "dog dog dog dog" should return false.| [word_pattern.cpp](leet_code_problems/word_pattern.cpp)|
76 changes: 76 additions & 0 deletions leet_code_problems/word_pattern.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
*
* 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.
* Examples:
* pattern = "abba", str = "dog cat cat dog" should return true.
* pattern = "abba", str = "dog cat cat fish" should return false.
* pattern = "aaaa", str = "dog cat cat dog" should return false.
* pattern = "abba", str = "dog dog dog dog" should return false.
* Notes:
* You may assume pattern contains only lowercase letters, and str
* contains lowercase letters separated by a single space.
*
* Approach:
*
* Split the string into words, and then map it to each character of the patterns as _we move.
* If the character is being mapped to multiple words, or a word is being mapped to multiple
* character, then the str is not following pattern.
*/

#include <iostream>
#include <vector>
#include <sstream>
#include <unordered_map>
#include <unordered_set>

bool match_pattern(const std::string& str, const std::string& pattern)
{
// split string to words
std::istringstream iss(str);
std::vector<std::string> words(std::istream_iterator<std::string>{iss},
std::istream_iterator<std::string>());
if (words.size() != pattern.size()) {
return false;
}

std::unordered_map<char, std::string> map;
std::unordered_set<std::string> set;
for (int i = 0; i < pattern.size(); ++i) {
if (map.find(pattern[i]) != map.end()) {
// pattern is already mapped to some other string.
if (words[i] != map[pattern[i]]) {
return false;
}
} else {
// word is already mapped to some other string.
if (set.count(words[i])) {
return false;
} else {
map[pattern[i]] = words[i];
set.insert(words[i]);
}
}
}
return true;
}

int main() {
std::string pattern{"abba"};
std::string str{"dog cat cat dog"};
std::cout << "Pattern : " << pattern << std::endl;
std::cout << "String :" << str << std::endl;
bool r = match_pattern(str, pattern);
std::string result = (r) ? "true" : "false";
std::cout << "Result :" << result << std::endl;

pattern = "aaaa";
std::cout << "Pattern : " << pattern << std::endl;
std::cout << "String :" << str << std::endl;
r = match_pattern(str, pattern);
result = (r) ? "true" : "false";
std::cout << "Result :" << result << std::endl;


return 0;
}

0 comments on commit 280420a

Please sign in to comment.