当前位置:网站首页>【890. 查找和替换模式】
【890. 查找和替换模式】
2022-06-12 22:19:00 【Sugar_wolf】
来源:力扣(LeetCode)
描述:
你有一个单词列表 words
和一个模式 pattern
,你想知道 words
中的哪些单词与模式匹配。
如果存在字母的排列 p
,使得将模式中的每个字母 x
替换为 p(x)
之后,我们就得到了所需的单词,那么单词与模式是匹配的。
(回想一下,字母的排列是从字母到字母的双射:每个字母映射到另一个字母,没有两个字母映射到同一个字母。)
返回 words
中与给定模式匹配的单词列表。
你可以按任何顺序返回答案。
示例:
输入:words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
输出:["mee","aqq"]
解释:
"mee" 与模式匹配,因为存在排列 {
a -> m, b -> e, ...}。
"ccc" 与模式不匹配,因为 {
a -> c, b -> c, ...} 不是排列。
因为 a 和 b 映射到同一个字母。
提示:
1 <= words.length <= 50
1 <= pattern.length = words[i].length <= 20
方法:构造双射
我们可以逐个判断 words
中的每个单词 word
是否与 pattern
匹配。
根据题意,我们需要构造从字母到字母的双射,即 word
的每个字母需要映射到 pattern
的对应字母,并且 pattern
的每个字母也需要映射到 word
的对应字母。
我们可以编写一个函数 match(word,pattern)
,仅当 word
中相同字母映射到 pattern
中的相同字母时返回 true
。我们可以在遍历这两个字符串的同时,用一个哈希表记录 word
的每个字母 x 需要映射到 pattern
的哪个字母上,如果 x 已有映射,则需要检查对应字母是否相同。
如果 match(word,pattern)
和 match(pattern,word)
均为 true
,则表示 word
与 pattern
匹配。
代码:
class Solution {
bool match(string &word, string &pattern) {
unordered_map<char, char> mp;
for (int i = 0; i < word.length(); ++i) {
char x = word[i], y = pattern[i];
if (!mp.count(x)) {
mp[x] = y;
} else if (mp[x] != y) {
// word 中的同一字母必须映射到 pattern 中的同一字母上
return false;
}
}
return true;
}
public:
vector<string> findAndReplacePattern(vector<string> &words, string &pattern) {
vector<string> ans;
for (auto &word: words) {
if (match(word, pattern) && match(pattern, word)) {
ans.emplace_back(word);
}
}
return ans;
}
};
执行用时:4 ms, 在所有 C++ 提交中击败了66.05%的用户
内存消耗:8.2 MB, 在所有 C++ 提交中击败了59.88%的用户
复杂度分析
时间复杂度: O(nm),其中 n 是数组 words 的长度,m 是 pattern 的长度。对于每个 word 需要 O(m) 的时间检查其是否与 pattern 匹配。
空间复杂度: O(m)。哈希表需要 O(m) 的空间。
author:LeetCode-Solution
边栏推荐
- [machine learning] learning notes 01- introduction
- 打新债开户安全么,新手该怎么操作?
- The interface testing tool apipos3.0 is applicable to process testing and reference parameter variables
- Ansible PlayBook et ansible roles (3)
- June training (day 10) - bit operation
- 大学期间零基础如何开展编程学习
- QT quick 3D learning: use mouse and keyboard to control node position and direction
- JVM foundation - > talk about class loader two parent delegation model
- USB机械键盘改蓝牙键盘
- JVM foundation > G1 garbage collector
猜你喜欢
Ansible foundation and common modules (I)
The programmer dedicated to promoting VIM has left. Father of vim: I will dedicate version 9.0 to him
[image denoising] image denoising based on trilateral filter with matlab code
Ansible playbook and ansible roles (III)
Leetcode: the maximum number of building change requests that can be reached (if you see the amount of data, you should be mindless)
SQL tuning guide notes 15:controlling the use of optimizer statistics
C语言:如何给全局变量起一个别名?
(downloadable) Research Report on the development and utilization of government data (2021), a glimpse of the development of Government Office
Prefix sum and difference
JVM Basics - > What are the thread shared areas in the JVM
随机推荐
【图像去噪】基于三边滤波器实现图像去噪附matlab代码
What are thread scheduler and timeslicing?
Pat grade A - 1167 Cartesian tree (30 points) (buildtree + level traversal)
How do I create a daemon thread? And where to use it?
[simple] 155 Minimum stack
QT quick 3D learning: mouse picking up objects
Ansible summary (VI)
Logstash timestamp converted to UNIX nanosecond nano second time
February 27th
The interface testing tool apipos3.0 is applicable to process testing and reference parameter variables
Modstartcms modular station building system v3.3.0 component function upgrade, event triggering enhancement
【LeetCode】300.最长上升子序列
Use group_ Dplyr issues when using group_ by(multiple variables)
疼痛分级为什么很重要?
[probability theory and mathematical statistics] final review: formula summary and simple examples (end)
JVM foundation - > three ⾊ mark
生成小程序菊花码(生成菊花码、更换中间logo、更改图片尺寸,加文字)
PE安装win10系统
"Oracle database parallel execution" technical white paper reading notes
One article to quickly understand whether there are security risks in your network