diff --git a/README.md b/README.md index 1d4646b..df8bfa2 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ | Current Status| Stats | | :------------: | :----------: | -| Total Problems | 180 | +| Total Problems | 181 | @@ -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)| diff --git a/leet_code_problems/word_pattern.cpp b/leet_code_problems/word_pattern.cpp new file mode 100644 index 0000000..2b5e64f --- /dev/null +++ b/leet_code_problems/word_pattern.cpp @@ -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 +#include +#include +#include +#include + +bool match_pattern(const std::string& str, const std::string& pattern) +{ + // split string to words + std::istringstream iss(str); + std::vector words(std::istream_iterator{iss}, + std::istream_iterator()); + if (words.size() != pattern.size()) { + return false; + } + + std::unordered_map map; + std::unordered_set 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; +} \ No newline at end of file