当前位置:网站首页>[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
边栏推荐
- Illustrate the significance of hashcode
- 常用编程记录——parser = argparse.ArgumentParser()
- Problems and solutions of database migration
- Common programming records - parser = argparse ArgumentParser()
- Integration and induction of knowledge points of automatic control principle (Han min version)
- Fact final variable and final variable
- Troubleshooting of data deleted and reappeared problems
- Integers have friends interval GCD + double pointer
- Numpy common function table sorting of data processing
- Contrôle automatique (version Han min)
猜你喜欢
如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images
Anaconda installed pyqt5 and pyqt5 tools without designer Exe problem solving
Explain of MySQL optimization
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation
Kalman filter and inertial integrated navigation
container
Substring Inversion (Easy Version)
Practical operation - Nacos installation and configuration
Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()
随机推荐
Implementation of displaying database pictures to browser tables based on thymeleaf
Treatment of tensorflow sequelae - simple example record torch utils. data. dataset. Picture dimension problem when rewriting dataset
Why does the subscript of the array start from 0 instead of 1?
Development environment EAS login license modification
Create enterprise mailbox account command
MySQL best practices for creating tables
3. Continuous integer
10.Advance Next Round
The user name and password of users in the domain accessing the samba server outside the domain are wrong
Guaba and Computational Geometry
Installation and usage skills of idea
You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it
Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
Integers have friends interval GCD + double pointer
Contrôle automatique (version Han min)
Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
5.The Simple Problem
线代第四章-向量组的线性相关
The problem that the page will refresh automatically after clicking the submit button on the form is solved
Mysql database foundation