Leetcode Note: Word Pattern
I. Description
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"Shocould returntrue.
pattern = "abba",str = "dog cat cat fish"Shocould returnfalse.
pattern = "aaaa",str = "dog cat cat dog"Shocould returnfalse.
pattern = "abba",str = "dog dog dog dog"Shocould returnfalse.
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
Ii. Question Analysis
The general idea is to give a set of pattern and a string (str), determine whether the string matches the pattern, and give several examples.
As mentioned in the topic, pattern only consists of lower-case letters, and each word in the str string is separated by a single space character, each word in the string is composed of lowercase letters (many boundary conditions can be considered in this setting). The pattern and str strings do not contain any extra spaces; in pattern, each letter must match at least one word in the str string.
It is easy to think that Map is used to solve this problem.
Definemap Every time a new pattern is encountered, the pattern iskeyTo store corresponding wordsmap; EncounteredmapCheck the currentstrIs the wordmapThe value corresponding to the recording mode is the same. If there are different values, return directlyfalse.
This judgment method can solve the following problems:pattern = "abba",str = "dog cat cat fish"This situation.
However, using only one map cannot determine this situation:pattern = "abba",str = "dog dog dog dog"First, a is used as a new mode,SavedmapIn the second iteration, B acts as a new model, Also savedmap,dogAssigned to two different modes, which is obviously incorrect.
To solve this problem, we need twomap. Anothermap2 Used to record the correspondence between words and patterns.
In general, map is used to record the correspondence between pattern and word, but multiple pattern may correspond to the same word.
Map2 is used to record the correspondence between words and patterns, and adds the judgment of two maps to the judgment to distinguish all situations.
Iii. Sample Code
Class Solution {public: bool wordPattern (string pattern, string str) {unordered_map
Map; // The role of unordered_map to prevent the occurrence of different words but the same pattern
Map2; // prevents the occurrence of a vector with different patterns but with the same name as a word.
Vec; // store words one by one // The following operations are divided into str for multiple words (int I = 0, j = 0; I <str. size (); ++ I) {if (I = str. size ()-1) {string temp = str. substr (j, I-j + 1); vec. push_back (temp);} if (str [I] = '') {string temp = str. substr (j, I-j); vec. push_back (temp); j = I + 1 ;}} if (pattern. size ()! = Vec. size () return false; for (int I = 0; I <pattern. size (); ++ I) {// The current mode does not appear, and the corresponding word does not appear, the key-value pair is saved to the table if (map. find (pattern [I]) = map. end () & map2.find (vec [I]) = map2.end () {map. insert (make_pair (pattern [I], vec [I]); map2.insert (make_pair (vec [I], pattern [I]);} else if (map [pattern [I]! = Vec [I]) return false;} return true ;}};
Iv. Summary
A simple question, but it takes some time to implement it, and you need to pay attention to some details.