当前位置:网站首页>[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
边栏推荐
- 7.Domino piling
- Class loading and classloader understanding
- Kalman filter and inertial integrated navigation
- Optional best practices
- POJ - 2955 brackets interval DP
- Preparedstatement prevents SQL injection
- Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison
- 自动控制(韩敏版)
- On traversal of binary tree
- Event listener
猜你喜欢

Linear algebra Chapter 1 - determinant

Paper on LDCT image reconstruction: edge enhancement based transformer for medical image denoising

Implementation of displaying database pictures to browser tables based on thymeleaf

In depth understanding of the relationship between dncblevel and noise denoising in the paper

On traversal of binary tree

线性代数第一章-行列式

Preparedstatement prevents SQL injection

Anaconda installed pyqt5 and pyqt5 tools without designer Exe problem solving

Illustrate the significance of hashcode

Techniques et principes de détection
随机推荐
Treatment of tensorflow sequelae - simple example record torch utils. data. dataset. Picture dimension problem when rewriting dataset
Exception handling: grab and throw model
Contrôle automatique (version Han min)
MySQL occasional Caton
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation
DBCP usage
Programming training
container
RedHat realizes keyword search in specific text types under the directory and keyword search under VIM mode
Generate excel template (drop-down selection, multi-level linkage)
Collection and map thread safety problem solving
SQL injection
Chapter 4 of line generation - linear correlation of vector systems
IO multiplexing of 09 redis
深度学习基础——简单了解meta learning(来自李宏毅课程笔记)
LockSupport. Park and unpark, wait and notify
Create binary tree
Explain of MySQL optimization
Detection technology and principle
Paper on LDCT image reconstruction: edge enhancement based transformer for medical image denoising