diff --git a/OJ notes/pages/Leetcode Word-Pattern.md b/OJ notes/pages/Leetcode Word-Pattern.md index ec9781f..92e53cd 100644 --- a/OJ notes/pages/Leetcode Word-Pattern.md +++ b/OJ notes/pages/Leetcode Word-Pattern.md @@ -61,6 +61,48 @@ Here **follow** means a full match, such that there is a bijection between a let > [!summary] > This is a #string operation problem. +The main part is using two hash tables, to check for +mismatches, and using `istringstream` to read from string +Two hash tables: +- One is used to check for s has one and only bound word +- The other is used to check that the word is only stored + once + +Using one hash table is one way, which makes one way +unchecked. ### Solution + +```cpp +class Solution { +public: + bool wordPattern(string pattern, string s) { + unordered_map stoi; + unordered_map ctoi; + // convert string to stream + istringstream input(s); + string word; + int n = pattern.size(); + int id; + + for (id = 0; input >> word; id++) { + // cout<