当前位置:网站首页>890. 查找和替换模式
890. 查找和替换模式
2022-06-12 10:34:00 【anieoo】
原题链接:890. 查找和替换模式
Solution:
利用两个数组,在遍历字符串的时候,words[i]存储单词到模式串的映射,pattern[i]存储模式串到单词的映射。
class Solution {
public:
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
int n = words.size();
vector<string> res; //返回值
for(int i = 0;i < n;i++) {
if(check(words[i], pattern)) res.push_back(words[i]);
}
return res;
}
bool check(const string &str,const string &patt) {
int words[26],pattern[26];
memset(words, -1, sizeof(words));
memset(pattern, -1, sizeof(pattern));
for(int i = 0;i < str.size();i++) {
int st = str[i] - 'a';
int ts = patt[i] - 'a';
if(words[st] == -1 && pattern[ts] == -1) { //如果字母还没映射,储存字母间的映射关系
words[st] = ts;
pattern[ts] = st;
}
else if(words[st] != ts || pattern[ts] != st) { //判断映射关系是否正确
return false;
}
}
return true;
}
};边栏推荐
- See if you fall into the trap of "labeling" customers and users?
- 数组,整型,字符变量在全局和局部的存在形式
- [experiment] MySQL master-slave replication and read-write separation
- Leetcode2154. 将找到的值乘以 2(二分查找)
- How to play the 2022 Taobao 618 Super Cat Games? What are the strategies for the Super Cat Games
- 基于C#的安全聊天工具设计
- Pycharm view the current version of opencv
- How to play the 618 super cat games on Taobao? Here comes the introduction to the overall activities of the Super Cat Games
- 容器江湖的爱恨情仇
- How to play the 2022 Taobao 618 Super Cat Games? Playing skills of 2022 Taobao 618 Cat Games
猜你喜欢

分布式存储探索

Leetcode2154. 将找到的值乘以 2(二分查找)

Properties Chinese garbled code

A hundred secrets and a few secrets - Caesar encryption

Amélioration de la 3dsc par HSC

2022京东618预售定金怎么退?京东618定金能退吗?

AcWing 135. 最大子序和(前缀和 + 单调队列求定长区间最小值)

深度学习与CV教程(14) | 图像分割 (FCN,SegNet,U-Net,PSPNet,DeepLab,RefineNet)

Pseudo static setting of access database in win2008 R2 iis7.5

Malicious code analysis practice - lab06-01 exe~Lab06-04. Exe dynamic analysis
随机推荐
2022 JD 618 Comment rembourser le dépôt de pré - vente? Le dépôt JD 618 peut - il être remboursé?
PHP wechat payment V3 interface
[experiment] MySQL master-slave replication and read-write separation
Getting started with cloud API basics -- basic knowledge of picgo writing plug-ins
Leetcode2154. Multiply the found value by 2 (binary search)
MYSQL——内置函数
Love and hate in the Jianghu
PHP can load different new data methods for each refresh
PHP uses RSA segment encryption and decryption method
基于C#的安全聊天工具设计
Summary method of lamp environment deployment
CTF freshman cup PHP deserialization question - EzPop
Collation of common functions in JS
PHP generate schedule
A few secrets - a special day
ID obfuscation
Set SVG color
PHP Apple purchase verification steps
The solution of Lenovo notebook ThinkPad t440 WiFi unable to connect to the Internet
How to play the 618 super cat games on Taobao? Here comes the introduction to the overall activities of the Super Cat Games