当前位置:网站首页>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;
}
};
边栏推荐
- C语言:如何给全局变量起一个别名?
- JVM foundation - > talk about class loader two parent delegation model
- Photoshop:PS如何实现放大图片不模糊
- Is there any risk in opening a securities account? How to open an account safely?
- flutter系列之:flutter中常用的GridView layout详解
- JVM foundation - what is the process of loading > objects into the JVM, and then clearing them by GC?
- The interface testing tool apipos3.0 is applicable to process testing and reference parameter variables
- JVM Basics - > What are the JVM parameters?
- 【LeetCode】103. Zigzag sequence traversal of binary tree
- Common rendering pipeline grooming
猜你喜欢

Anti aliasing / anti aliasing Technology

管线中的坐标变换

Hostvars in ansible

【Web技术】1348- 聊聊水印实现的几种方式

微信小程序提现功能

Gb28181 protocol -- alarm

JVM Basics - > how to troubleshoot JVM problems in your project

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

Web3 principle and decentralization

基于51单片机的酒精检测仪
随机推荐
【LeetCode】33. Search rotation sort array
【LeetCode】33. 搜索旋转排序数组
Colab tutorial (super detailed version) and colab pro/colab pro+ usage evaluation
Theory + practice will help you master the dynamic programming method
【LeetCode】102. Sequence traversal of binary tree
Is it safe to open an account with new bonds? How should novices operate?
vim利用右下4键
【LeetCode】69. x 的平方根
USB mechanical keyboard changed to Bluetooth Keyboard
C语言:如何给全局变量起一个别名?
JVM foundation > G1 garbage collector
JVM Basics - > What are the JVM parameters?
The "fourteenth five year plan" development plan and panoramic strategic analysis report of China's information and innovation industry 2022 ~ 2028
lua 条件语句
[web technology] 1348- talk about several ways to implement watermarking
【LeetCode】53.最大子数组和
The fate of Internet people is that it is difficult to live to 30?
年薪50万是一条线,年薪100万又是一条线…...
Sword finger offer series - 47 Maximum value of gifts
Research Report on market supply and demand and strategy of China's digital camera lens industry