[LeetCode] 890. Find and Replace Pattern 查找和替换模式
You have a list of `words` and a `pattern`, and you want to know which words in `words` matches the pattern.
A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.
(Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.)
Return a list of the words in words that match the given pattern.
You may return the answer in any order.
Example 1:
Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"]
Explanation: "mee" matches the pattern because there is a permutation {a -> m, b -> e, ...}.
"ccc" does not match the pattern because {a -> c, b -> c, ...} is not a permutation,
since a and b map to the same letter.
Note:
1 <= words.length <= 501 <= pattern.length = words[i].length <= 20
这道题给了我们一个字符串数组 words,还有一个 pattern 单词,问 words 数组中的单词是否满足 pattern 的模式,并给了一个例子。比如 pattern 是 abb 的话,表示后两个字母是相同的,比如 mee 和 aqq,那么一个很直接的想法就是建立每个单词 word 和 pattern 中每个字符之间的映射,比如 mee->abb 的话,就是 m->a, e->b,在建立映射之前要判断,若已经存在了该映射,且映射值不是当前 pattern 中的对应字符时,就是无法匹配的,比如 mm 和 ab,在第一次建立了 m->a 的映射,当遍历到第二个m的时候,发现m的映射已经存在,但不是b,就不能再建立 m->b 的映射,则表示无法匹配。分析到这里,你可能感觉没啥问题,但其实我们忽略一种情况,word 和 pattern 中的每个字符必须是一一对应的,任何一个方向的多对一都是不行了,比如 mn 和 aa,刚开始建立了 m->a 的映射,遍历到n的时候,发现没有n的映射,此时也不能建立 n->a 的映射,因为 pattern 中的a已经被占用了,所以还需要一个 HashMap 来建立反方向的映射,只有两个 HashMap 中都不存在的,才能建立映射,只要有一个已经存在了,直接 break 掉。在 for 循环结束后,看是否已经到达了 word 的末尾,没有提前 break 掉的话,就将 word 加入结果 res 中即可,参见代码如下:
解法一:
class Solution {
public:
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
vector<string> res;
for (string word : words) {
unordered_map<char, char> w2p, p2w;
int i = 0, n = word.size();
for (; i < n; ++i) {
if (w2p.count(word[i]) && w2p[word[i]] != pattern[i]) break;
w2p[word[i]] = pattern[i];
if (p2w.count(pattern[i]) && p2w[pattern[i]] != word[i]) break;
p2w[pattern[i]] = word[i];
}
if (i == n) res.push_back(word);
}
return res;
}
};
我们也可以不用 HashMap,改用两个长度为 26 的数组,因为这道题貌似默认都是小写字母,唯一麻烦一点的就是要把字母减去 'a' 来转为对应的坐标,还有一点跟上面解法不同的地方就是,字母是跟起坐标位置加1来建立映射(加1的原因是默认值是0,而当 i=0 时为了区分默认值,就要加1),因为两个字母都跟一个特定的值相等,其实也等价于这两个字母之间建立的映射(a->c, b->c => a->b)。整体思路还是没啥不同的,参见代码如下:
解法二:
class Solution {
public:
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
vector<string> res;
for (string word : words) {
vector<int> w(26), p(26);
int i = 0, n = word.size();
for (; i < n; ++i) {
if (w[word[i] - 'a'] != p[pattern[i] - 'a']) break;
w[word[i] - 'a'] = p[pattern[i] - 'a'] = i + 1;
}
if (i == n) res.push_back(word);
}
return res;
}
};
在论坛上又看到了一种解法,这种解法相当于把所有的单词都转为了一种特定的模式,具体来说,就是用一个 HashMap,建立每个字母跟其之前出现过的字母种类个数之前的映射,比如 mee->011,aqq->011,这样相同的模式映射的值是一样的,具体的做法是若当前字母没有出现过,则建立和当前 HashMap 中的映射个数之间的映射,是一种很巧妙的设计思路,只不过最后又给每个数字加上了 'a',转为了字母的 pattern,即 mee->abb,aqq->abb,参见代码如下:
解法三:
class Solution {
public:
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
vector<string> res;
for (string word : words) {
if (helper(word) == helper(pattern)) res.push_back(word);
}
return res;
}
string helper(string word) {
unordered_map<char, int> m;
for (char c : word) {
if (!m.count(c)) m[c] = m.size();
}
for (int i = 0; i < word.size(); ++i) word[i] = 'a' + m[word[i]];
return word;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/890
类似题目:
参考资料:
https://leetcode.com/problems/find-and-replace-pattern/
https://leetcode.com/problems/find-and-replace-pattern/discuss/161266/JAVA-3ms-Clear-Code
[LeetCode All in One 题目讲解汇总(持续更新中...)](https://www.cnblogs.com/grandyang/p/4606334.html)
[LeetCode] 890. Find and Replace Pattern 查找和替换模式的更多相关文章
- Leetcode 890. Find and Replace Pattern
把pattern映射到数字,也就是把pattern标准化. 比如abb和cdd如果都能标准化为011,那么就是同构的. class Solution: def findAndReplacePatter ...
- 890. Find and Replace Pattern - LeetCode
Question 890. Find and Replace Pattern Solution 题目大意:从字符串数组中找到类型匹配的如xyy,xxx 思路: 举例:words = ["ab ...
- LC 890. Find and Replace Pattern
You have a list of words and a pattern, and you want to know which words in words matches the patter ...
- 【LeetCode】890. Find and Replace Pattern 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+set 单字典 日期 题目地址:https:/ ...
- [Swift]LeetCode890. 查找和替换模式 | Find and Replace Pattern
You have a list of words and a pattern, and you want to know which words in words matches the patter ...
- leetcode 890. 查找和替换模式 Python
用模式的每个字母去当做key对应单词列表的每个字母value, 如果放进dict之前检测到key已经存在,就检测Word[i][j]是否是和已经存在的value一致,不一致就代表不匹配,break检查 ...
- 890. Find and Replace Pattern找出匹配形式的单词
[抄题]: You have a list of words and a pattern, and you want to know which words in words matches the ...
- [LeetCode] Find And Replace in String 在字符串中查找和替换
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- 如何使用Replace Pioneer批量查找和替换并提取指定字符串
1 我们查看源代码之后获得的网页文件如下图所示,一般都是href="/p-286018571.html"我们只要能提取到所有的"/p-XXXXXXXX.html" ...
随机推荐
- servlet中的IllegalStateException
IllegalStateException在java web开发中比较常见,IllegalStateException的根本原因是java servlet在提交响应后,还尝试写内容. 所以避免Ille ...
- Uboot启动流程分析(二)
1.前言 在前面的文章Uboot启动流程分析(一)中,链接如下: https://www.cnblogs.com/Cqlismy/p/12000889.html 已经简单地分析了low_level_i ...
- Oracle 11g 服务端的安装步骤
Ø 简介 本文主要介绍 Oracle 11g 服务端的安装步骤,在介绍之前说明以下几点: 1. 所安装的服务器是本机的虚拟机,操作系统为 Windows Server 2019: 2. 以下 ...
- 汇总Anaconda与ROS冲突解决方法
汇总一下在网上找到的Anaconda与ROS冲突解决方法,如果还有其他人找到其他方法,欢迎留言. anaconda和ros的安装就不介绍了. 1. 在某视频网站上一个印度小哥提出的方法 安装完成后,在 ...
- Javascript 实现倒计时效果
代码来自于网上. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...
- AOP方法拦截获取参数上的注解
https://www.jianshu.com/p/f5c7417a75f9 获取参数注解 在spring aop中,无论是前置通知的参数JoinPoint,还是环绕通知的参数ProceedingJo ...
- Quartz.net任务调度
一.Quartz.net简介 Quartz.net是一个开源的任务调度框架,很多定时任务.调度任务都可以用这个框架,如定时日志等. 二.Quartz.net用途 定时给女朋友发送消息 女朋友生日的时候 ...
- Java常用类Date相关知识
Date:类 Date 表示特定的瞬间,精确到毫秒. 在 JDK 1.1 之前,类 Date 有两个其他的函数.它允许把日期解释为年.月.日.小时.分钟和秒值.它也允许格式化和解析日期字符串. Dat ...
- 关于项目中js原型的使用
在一个项目中为了减少全局变量的使用及模块化的开发我们使用的构造函数加原型的开发模式 var App = function(){ //管理构造函数的属性 this.name = 'jack' } //页 ...
- IOS之UIColor
转自:http://blog.csdn.net/wudizhukk/article/details/8607229 UIColor常见用法,废话少说 直接网上抄来记录下,凭空想还真有点想不起来,最近记 ...