当前位置:网站首页>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;
}
};边栏推荐
- Malicious code analysis practice - lab03-03 Exe basic dynamic analysis
- 2021-03-24
- 浅谈三维形状上下文特征3DSC理论及应用
- 卡鱼刺别再喝醋吞米饭了!教你2招,让鱼刺安全“跑出来”
- Common methods of string class
- Chapter 3 search
- Golang start service background daemon
- 验收标准到底是不是测试用例?
- AcWing 135. 最大子序和(前缀和 + 单调队列求定长区间最小值)
- Get start date and end date for all quarters of the year
猜你喜欢
![[experiment] MySQL master-slave replication and read-write separation](/img/aa/7d0799013ff749cacf44ba3b773dff.png)
[experiment] MySQL master-slave replication and read-write separation

Student management system

AcWing 132. 小组队列(队列模拟题)

Getting started with cloud API basics -- basic knowledge of picgo writing plug-ins

How to refund the pre-sale deposit of JD 618 in 2022? Can JD 618 deposit be refunded?

Valentina Studio Pro for Mac(mac数据库管理软件)

Leetcode 2169. 得到 0 的操作数

Malicious code analysis practice - lab06-01 exe~Lab06-04. Exe dynamic analysis

A hundred secrets and a few secrets - Caesar encryption

Malicious code analysis practice - use IDA pro to analyze lab05-01 dll
随机推荐
Leetcdoe 2037. Make each student have the minimum number of seat movements (yes, once)
PHP: Excel to get the letter header
Pseudo static setting of access database in win2008 R2 iis7.5
Download Notepad++
The difference between static method locking and non static method locking
MYSQL——内置函数
Global and local existence of array, integer and character variables
PHP uses leanclound to save associated properties
Binassii module - converting between binary and ASCII
Malicious code analysis practice - lab03-03 Exe basic dynamic analysis
The name of a great man
Wechat payment, wechat refund, Alibaba payment
Using the echart plug-in to dynamically refresh charts in uview/uni-app
QT custom window fillets
Common methods of string class
浅谈三维形状上下文特征3DSC理论及应用
One test for twoorthree years, recording some thoughts on test exchange experience
Assign a specified amount to a specified number of people at random
M-Arch(番外11)GD32L233评测-PWM驱动有源蜂鸣器
2022淘宝618超级喵运会怎么玩?2022淘宝618喵运会玩法技巧