当前位置:网站首页>LeetCode 890 查找和替换模式[map] HERODING的LeetCode之路
LeetCode 890 查找和替换模式[map] HERODING的LeetCode之路
2022-06-12 22:47:00 【HERODING23】

解题思路:
本质上是解决一个双向映射的问题,即对于每个word和pattern中的每个字母,都要双向映射,比如体重的mee和abb,必须e和b对应,且b和e对应,这就需要构造两个数组来分别存储word中对应字母下标和pattern中对应字母下标的映射,以及pattern中对应字母下标与word中对应字母下标的映射,如果都能对应上,则word满足匹配模式,放入res数组中,否则直接break,代码如下:
class Solution {
public:
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
vector<string> res;
int len = pattern.size();
for(auto& word : words) {
vector<int> reflect1(26, -1);
vector<int> reflect2(26, -1);
int flag = true;
for(int i = 0; i < len; i ++) {
if(reflect1[word[i] - 'a'] == -1 && reflect2[pattern[i] - 'a'] == -1) {
reflect1[word[i] - 'a'] = pattern[i] - 'a';
reflect2[pattern[i] - 'a'] = word[i] - 'a';
} else {
if(reflect1[word[i] - 'a'] == pattern[i] - 'a' && reflect2[pattern[i] - 'a'] == word[i] - 'a') {
continue;
} else {
flag = false;
break;
}
}
}
if(flag) {
res.emplace_back(word);
}
}
return res;
}
};
边栏推荐
- QT quick 3D learning: mouse picking up objects
- Generate the chrysanthemum code of the applet (generate the chrysanthemum code, change the middle logo, change the image size, and add text)
- Research Report on truffle fungus industry - market status analysis and development prospect forecast
- China's elastic belt market trend report, technical dynamic innovation and market forecast
- csredis-in-asp. Net core theory practice - use examples
- [890. find and replace mode]
- China barcode decoder market trend report, technical innovation and market forecast
- China embolic coil market trend report, technical innovation and market forecast
- flutter系列之:flutter中常用的GridView layout详解
- China's alternative sports equipment market trend report, technology dynamic innovation and market forecast
猜你喜欢

Qt Quick 3D学习:鼠标拾取物体

C # reading table data in word
![[web technology] 1348- talk about several ways to implement watermarking](/img/5f/c4f6ba6799202c79d1e9cb7a083952.png)
[web technology] 1348- talk about several ways to implement watermarking

2022 heavyweight: growth law - skillfully use digital marketing to break through enterprise difficulties

ShardingSphere-proxy-5.0.0部署之分表实现(一)

JVM foundation > CMS garbage collector

Mysql case when then函数使用

RAID disk array

web3 原则和去中心化

QT quick 3D learning: mouse picking up objects
随机推荐
Anti aliasing / anti aliasing Technology
JVM foundation > GC generation: minorgc majorgc fullgc mixed GC
Generate the chrysanthemum code of the applet (generate the chrysanthemum code, change the middle logo, change the image size, and add text)
be careful! Your Navicat may have been poisoned
Insight into China's smart medical industry in 2022
Hostvars in ansible
C语言:如何给全局变量起一个别名?
PHP删除二维数组中相同项的数据
Inventory of CV neural network models from 2021 to 2022
The programmer dedicated to promoting VIM has left. Father of vim: I will dedicate version 9.0 to him
42岁大厂高管,给30岁-39岁人提个醒:这6个让你变强的习惯,要尽快养成
Is it safe to open an account in flush? How to open an account online to buy stocks
Research Report on water sports shoes industry - market status analysis and development prospect forecast
The annual salary of 500000 is one line, and the annual salary of 1million is another line
JVM foundation - what is the process of loading > objects into the JVM, and then clearing them by GC?
Lua loop statement
vim利用右下4键
JVM Basics - > What are the JVM parameters?
Research Report on market supply and demand and strategy of China's digital camera lens industry
【LeetCode】5. Longest Palindromic Substring