当前位置:网站首页>[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
边栏推荐
- 5.The Simple Problem
- Customized communication between threads (reentrantlock)
- 深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索
- Kibana search syntax
- Integration and induction of knowledge points of automatic control principle (Han min version)
- 深度学习基础——简单了解meta learning(来自李宏毅课程笔记)
- MySQL advanced query
- 8. Integer Decomposition
- Understanding and use of tp50, tp90 and tp99
- 自动控制原理知识点整合归纳(韩敏版)
猜你喜欢

线性代数第二章-矩阵及其运算
![Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention](/img/4e/1a51636853d11544e6f5c37a588730.png)
Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention

自動控制(韓敏版)

检测技术与原理

深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索

Pytoch -- data loading and processing

St table template

图像恢复论文简记——Uformer: A General U-Shaped Transformer for Image Restoration

Techniques et principes de détection

C language file operation
随机推荐
IO multiplexing of 09 redis
Generate excel template (drop-down selection, multi-level linkage)
POI and easyexcel exercises
檢測技術與原理
PyTorch笔记——观察DataLoader&用torch构建LeNet处理CIFAR-10完整代码
自動控制(韓敏版)
Stability building best practices
Contrôle automatique (version Han min)
Common sense of thread pool
A general U-shaped transformer for image restoration
Pyqy5 learning (2): qmainwindow + QWidget + qlabel
Substring Inversion (Easy Version)
Preparedstatement prevents SQL injection
图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi
Addition, deletion, modification and query of MySQL table
Pytorch introduction notes - use a simple example to observe the output size of each layer of forward propagation
线性代数第三章-矩阵的初等变换与线性方程组
Calculation (enter the calculation formula to get the result)
Three ways to create threads
卡尔曼滤波与惯性组合导航