当前位置:网站首页>[leetcode 290] word rules
[leetcode 290] word rules
2022-04-23 06:23:00 【Don't steal my energy】
Title Description
Give a rule pattern And a string str , Judge str Follow the same rule .
there follow Finger perfect match , for example , pattern Each letter and string in str There is a two-way connection between each non empty word in .
Example 1
Input : pattern = "abba", str = "dog cat cat dog"
Output : true
Example 2
Input :pattern = "abba", str = "dog cat cat fish"
Output : false
Example 3
Input : pattern = "aaaa", str = "dog cat cat dog"
Output : false
Tips :
- 1 <= pattern.length <= 300
- pattern Only lowercase letters
- 1 <= s.length <= 3000
- s Only lowercase letters and ’ ’
- s It doesn't contain Any leading or trailing pair of spaces
- s Every word in the is Single space Separate
Their thinking
C++ Using hash table to realize double mapping , That is, string pattern The elements in and str The words in , They are exchanged and stored in the hash table as keywords and values , Then you get two hash tables (A and B), Then the comparison process , Make judgment in the process of comparison , If the current key appears , But if the values are different, it returns 0. The reader doesn't understand for a moment , The comparison process of the following two examples can be simulated manually on paper .
Ideas
for(int i=0;i<pattern.length();i++)
{
if( A.find( s pass the civil examinations i 's words )!=A.end() && pattern pass the civil examinations i The letter of !=A[ s pass the civil examinations i 's words ])//find Function to determine whether a key exists
return 0;
if( B.find(pattern pass the civil examinations i The letter of )!=B.end() && s pass the civil examinations i 's words !=B[ pattern pass the civil examinations i The letter of ])
return 0;
}
Example 1
Hashtable A
| key | a | b | b | a |
|---|---|---|---|---|
| value | dog | cat | cat | dog |
Hashtable B
| key | dog | cat | cat | dog |
|---|---|---|---|---|
| value | a | b | b | a |
Example 3
Hashtable A
| key | a | a | a | a |
|---|---|---|---|---|
| value | dog | cat | cat | dog |
Hashtable B
| key | dog | cat | cat | dog |
|---|---|---|---|---|
| value | a | a | a | a |
The code is as follows :
// Rules of words
bool wordPattern(string pattern, string s) {
unordered_map<string,char>HashStr;
unordered_map<char,string>Hashpattern;
vector<string> ans;// Store string s The words in
string str="";
for(int i=0;i<s.length();i++){
if(s[i]!=' ')
str+=s[i];
else{
ans.push_back(str);
str="";
}
}
ans.push_back(str); // here ans The elements in Namely s The words in
if(pattern.length()!=ans.size())// Determine whether the number matches
return 0;
for(int i=0;i<pattern.length();i++)// Exchange the elements of two strings as keys and values, respectively Insert into hash table
{
HashStr[ans[i]]=pattern[i];
Hashpattern[pattern[i]]=ans[i];
}
for(int i=0;i<pattern.length();i++)// The essence is
{
if(HashStr.find(ans[i])!=HashStr.end()&&pattern[i]!=HashStr[ans[i]])//find Function to query whether there exists
return 0;
if(Hashpattern.find(pattern[i])!=Hashpattern.end()&&ans[i]!=Hashpattern[pattern[i]])
return 0;
}
return 1;
}
This question mainly tests the operation of hash table , Readers can practice .
版权声明
本文为[Don't steal my energy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617012135.html
边栏推荐
- Linear algebra Chapter 1 - determinant
- The attendance client date of K / 3 wise system can only be selected to 2019
- 2. Average length of words
- Techniques et principes de détection
- Why does the subscript of the array start from 0 instead of 1?
- Pytorch introduction notes - use a simple example to observe the output size of each layer of forward propagation
- 图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi
- Use of multithreaded executors
- MySQL best practices for creating tables
- Chapter 4 of line generation - linear correlation of vector systems
猜你喜欢

Explain of MySQL optimization

Mysql database foundation
![图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi](/img/1b/4eea05e2634780f45b44273d2764e3.png)
图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi

Algèbre linéaire chapitre 1 - déterminants

Automatic control (Han min version)

C language file operation
![Comparative study paper - [Moco, cvpr2020] momentum contract for unsupervised visual representation learning](/img/21/4bc94fe29f416c936436c04fc16fa8.png)
Comparative study paper - [Moco, cvpr2020] momentum contract for unsupervised visual representation learning

Guaba and Computational Geometry

A general U-shaped transformer for image restoration

自动控制(韩敏版)
随机推荐
Advanced operation of idea debug
List segmentation best practices
線性代數第一章-行列式
Understanding and use of tp50, tp90 and tp99
Fact final variable and final variable
Chapter 4 of line generation - linear correlation of vector systems
去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots
常用编程记录——parser = argparse.ArgumentParser()
RedHat realizes keyword search in specific text types under the directory and keyword search under VIM mode
String notes
10.Advance Next Round
Complete example demonstration of creating table to page - joint table query
Automatic control (Han min version)
如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation
对比学习论文——[MoCo,CVPR2020]Momentum Contrast for Unsupervised Visual Representation Learning
How does MySQL convert stored seconds into dates
Doomsday (simple computational geometry)
Create enterprise mailbox account command
Fundamentals of in-depth learning -- a simple understanding of meta learning (from Li Hongyi's course notes)
去噪论文——[Noise2Void,CVPR19]Noise2Void-Learning Denoising from Single Noisy Images