# Word rule LeetCode290

## thinking

Seeing this topic, I subconsciously thought of hash_map, because this one-to-one correspondence is not hash_ What's your specialty?

1) At the beginning, I only thought of the mapping from pattern to str. first, STR is segmented by simulation and stored in the middle of VEC < string > array

2) But it ignores eg:pattern="abba"     str = "dog dog dog dog" in this case, we need to establish not only the mapping from pattern to str, but also the mapping from str to pattern! That is, you need to create a hash twice_ Map, but the logic is basically the same

3) There is also a special pitfall in the dataset: pattern=str="jquery"       wtf... This situation is also against the rules. We can understand it as J - > jQuery   , But Q, u, e, R and y all correspond to NULL, which is not allowed!

Therefore, in order to fill the situation in 3), we need to add a judgment logic after dividing str -- if pattern. Size()= VEC. Size(), indicating that some characters are mapped and some characters are not mapped, then false is returned directly, indicating that it is not allowed!

```#include<unordered_map>
#include<string>
#include<iostream>
using namespace std;

class Solution {
public:
bool wordPattern(string pattern, string s) {
int flag = 0;
for (int i = 0; i < pattern.size(); ++i) {
if (pattern[i] != s[i]) {
flag = 1; break;
}
}
if (flag == 0) return true;
unordered_map<char, string> m;
unordered_map<string, char> mua;
vector<string> vec;
for (int i = 0; i < s.size(); ++i) {
if (s[i] == ' ') continue;
int j = i;
while (s[j] != ' '&&j<s.size()) j++;
string temp = string(s.begin()+i, s.begin() + j);
i = j;
cout << temp;
vec.push_back(temp);
}
//Here, you must judge the special situation: "query" "query"
for (int i = 0; i < pattern.size(); ++i) {
if (m.find(pattern[i]) != m.end()) {
string temp = m[pattern[i]];
for (int j = 0; j < vec[i].size(); ++j) {
if (temp[j] != vec[i][j])  return false;
}
}
else {
m[pattern[i]] = vec[i];
}
}
for (int i = 0; i < vec.size(); ++i) {
if (mua.find(vec[i]) != mua.end()) {
char temp = mua[vec[i]];
if (temp != pattern[i])    return false;
}
else {
mua[vec[i]] = pattern[i];
}
}
return true;
}
};

int main()
{
Solution s;
string s1, s2;
cin >> s1;
char temp;
getchar();
getline(cin,s2);
bool flag;
flag=s.wordPattern(s1, s2);
cout << flag;
return 0;
}```

result:

### supplement

During this process, I found that I had forgotten a lot of basic knowledge of C + +. I hereby record it as a supplement:

A: the header file written by yourself must be suffixed with. h, but if C + + uses a function library from C, eg:stdlib.h   So when we write c + + programs, we need to add a C before stdlib and write it as eg:cstdlib     Indicates that the header file is from the C language function library, but if we add. h to indicate that the namespace of the C language library is used (interested students can search by themselves), we don't need to write using namespace STD; This sentence, but not recommended! Custom namespace is recommended!

There is a special header file, the string.h header file of C language and the string header file of C + +. These two files seem to be expanded, but in fact, these two header files have nothing to do with each other! The string.h header file defines many string operation functions of char * data type (such as strcpy), while the C++string header file defines a new data result string! String related operation function!

2)getline(cin,str)

This function is defined in the C++string header file! Can make str input with spaces!

3)getchar()

This function is defined in the C language stdio header file! Be able to input line breaks left over from the last input

Tags: Algorithm leetcode

Posted on Tue, 30 Nov 2021 01:47:03 -0500 by wherertheskips