Word Pattern Solutions in C++
Number 290
Difficulty Easy
Acceptance 37.0%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/word-pattern/// Author : Hao Chen// Date : 2015-10-22class Solution {private::vector<string> split(string str, char delimiter) {vector<string> internal;stringstream ss(str); // Turn the string into a stream.string tok;while(getline(ss, tok, delimiter)) {internal.push_back(tok);}return internal;}public:bool wordPattern(string pattern, string str) {unordered_map<char, string> dict_pattern;unordered_map<string, char> dict_string;int len = pattern.size();vector<string> strs = split(str, ' ');if (len != strs.size()) return false;for(int i=0; i<len; i++) {char& ch = pattern[i];string& s = strs[i];if ( dict_pattern.find(ch) == dict_pattern.end() ) {dict_pattern[ch] = s;}if ( dict_string.find(s) == dict_string.end() ) {dict_string[s] = ch;}if ( dict_pattern[ch] != s || dict_string[s] != ch ) {return false;}}return true;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/word-pattern/description//// Author : liuyubobobo/// Time : 2018-07-03#include <iostream>#include <vector>#include <unordered_map>#include <string>using namespace std;/// HashMap/// TimeComplexity: O(n)/// Space Complexity: O(n)class Solution {public:bool wordPattern(string pattern, string str) {vector<string> words = split(str);if(pattern.size() != words.size())return false;unordered_map<char, string> map1;unordered_map<string, char> map2;for(int i = 0 ; i < pattern.size() ; i++)if(map1.find(pattern[i]) == map1.end()){if(map2.find(words[i]) != map2.end())return false;map1[pattern[i]] = words[i];map2[words[i]] = pattern[i];}else{string s = map1[pattern[i]];if(s != words[i])return false;}return true;}private:vector<string> split(const string& s){vector<string> res;int start = 0;for(int i = start + 1 ; i <= s.size() ; )if(i == s.size() || s[i] == ' '){res.push_back(s.substr(start, i - start));start = i + 1;i = start + 1;}elsei ++;return res;}};void print_bool(bool res){cout << (res ? "True" : "False") << endl;}int main() {string pattern1 = "abba";string str1 = "dog cat cat dog";print_bool(Solution().wordPattern(pattern1, str1));string pattern2 = "abba";string str2 = "dog cat cat fish";print_bool(Solution().wordPattern(pattern2, str2));string pattern3 = "aaaa";string str3 = "dog cat cat dog";print_bool(Solution().wordPattern(pattern3, str3));string pattern4 = "abba";string str4 = "dog dog dog dog";print_bool(Solution().wordPattern(pattern4, str4));return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/word-pattern/description//// Author : liuyubobobo/// Time : 2018-07-03#include <iostream>#include <vector>#include <unordered_map>#include <string>using namespace std;/// HashMap to record the position of each character in pattern and word in str/// TimeComplexity: O(n)/// Space Complexity: O(n)class Solution {public:bool wordPattern(string pattern, string str) {vector<string> words = split(str);if(pattern.size() != words.size())return false;unordered_map<char, int> map1;unordered_map<string, int> map2;for(int i = 0 ; i < pattern.size() ; i++){unordered_map<char, int>::iterator iter1 = map1.find(pattern[i]);unordered_map<string, int>::iterator iter2 = map2.find(words[i]);int i1 = (iter1 == map1.end() ? -1 : map1[pattern[i]]);int i2 = (iter2 == map2.end() ? -1 : map2[words[i]]);if(i1 != i2)return false;map1[pattern[i]] = i;map2[words[i]] = i;}return true;}private:vector<string> split(const string& s){vector<string> res;int start = 0;for(int i = start + 1 ; i <= s.size() ; )if(i == s.size() || s[i] == ' '){res.push_back(s.substr(start, i - start));start = i + 1;i = start + 1;}elsei ++;return res;}};void print_bool(bool res){cout << (res ? "True" : "False") << endl;}int main() {string pattern1 = "abba";string str1 = "dog cat cat dog";print_bool(Solution().wordPattern(pattern1, str1));string pattern2 = "abba";string str2 = "dog cat cat fish";print_bool(Solution().wordPattern(pattern2, str2));string pattern3 = "aaaa";string str3 = "dog cat cat dog";print_bool(Solution().wordPattern(pattern3, str3));string pattern4 = "abba";string str4 = "dog dog dog dog";print_bool(Solution().wordPattern(pattern4, str4));return 0;}