【LeetCode】890. Find and Replace Pattern 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/find-and-replace-pattern/description/
题目描述
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 <= 50
- 1 <= pattern.length = words[i].length <= 20
题目大意
我觉得应该把题目中的permutation理解为“映射”更为合适。
这样的话,题目意思就是找出words中所有满足映射关系的单词。映射关系由pattern给出,即要求words中的单词和pattern中的每个字符的对应关系应该完全一致。
解题方法
字典+set
既然考到映射,那么应该是使用HashMap来搞定,直接把映射一一的放入map中,如果出现过这个映射的话,就看新的对应关系和原来的映射是否相同。
代码中使用了set,这个set很重要,因为这个保证了不会出现ccc对应abb这种。
注意题目中给出了一个细节,就是words里面的每个word都和pattern长度相同,省去了判断长度的过程。
代码如下:
class Solution(object):
def findAndReplacePattern(self, words, pattern):
"""
:type words: List[str]
:type pattern: str
:rtype: List[str]
"""
ans = []
set_p = set(pattern)
for word in words:
if len(set(word)) != len(set_p):
continue
fx = dict()
equal = True
for i, w in enumerate(word):
if w in fx:
if fx[w] != pattern[i]:
equal = False
break
fx[w] = pattern[i]
if equal:
ans.append(word)
return ans
单字典
如果使用单字典,需要判断word向pattern的映射和pattern向word的映射。这个代码就没有什么好说的了。
Python代码如下:
class Solution(object):
def findAndReplacePattern(self, words, pattern):
"""
:type words: List[str]
:type pattern: str
:rtype: List[str]
"""
res = []
for word in words:
if len(word) != len(pattern): continue
d = dict()
isMatch = True
for i, c in enumerate(pattern):
if c in d:
if d[c] != word[i]:
isMatch = False
break
d[c] = word[i]
d = dict()
for i, c in enumerate(word):
if c in d:
if d[c] != pattern[i]:
isMatch = False
break
d[c] = pattern[i]
if isMatch:
res.append(word)
return res
C++代码如下:
class Solution {
public:
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
vector<string> res;
for (string& word : words) {
bool isMatch = true;
map<char, char> d;
for (int i = 0; i < word.size(); i++) {
if (d.count(word[i])) {
if (d[word[i]] != pattern[i]) {
isMatch = false;
break;
}
}
d[word[i]] = pattern[i];
}
d.clear();
for (int i = 0; i < word.size(); i++) {
if (d.count(pattern[i])) {
if (d[pattern[i]] != word[i]) {
isMatch = false;
break;
}
}
d[pattern[i]] = word[i];
}
if (isMatch) res.push_back(word);
}
return res;
}
};
日期
2018 年 8 月 24 日 —— Keep fighting!
2018 年 11 月 5 日 —— 打了羽毛球,有点累
2018 年 12 月 2 日 —— 又到了周日
【LeetCode】890. Find and Replace Pattern 解题报告(Python & C++)的更多相关文章
- [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 patter ...
- 【LeetCode】459. Repeated Substring Pattern 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历子串 日期 [LeetCode] 题目地址:ht ...
- Leetcode 890. Find and Replace Pattern
把pattern映射到数字,也就是把pattern标准化. 比如abb和cdd如果都能标准化为011,那么就是同构的. class Solution: def findAndReplacePatter ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ...
- 【LeetCode】456. 132 Pattern 解题报告(Python)
[LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 890. Find and Replace Pattern - LeetCode
Question 890. Find and Replace Pattern Solution 题目大意:从字符串数组中找到类型匹配的如xyy,xxx 思路: 举例:words = ["ab ...
- 【LeetCode】468. Validate IP Address 解题报告(Python)
[LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
随机推荐
- php5.6升级7
1. 检查当前安装的 PHP查看当前 PHP 版本 php -v查看当前 PHP 相关的安装包 yum list installed | grep php2. 更换 RPM 源#Centos 5.X: ...
- ZooKeeper 04 - ZooKeeper 集群的节点为什么必须是奇数个
目录 1 - 关于节点个数的说明 2 - ZooKeeper 集群的容错数 3 - ZooKeeper 集群可用的标准 4 - 为什么不能是偶数个节点 4.1 防止由脑裂造成的集群不可用 4.2 奇数 ...
- 学习java 7.11
学习内容: 泛型定义格式:<类型> 优点:把运行时期的问题提前到编译期间:避免了强制类型转换 泛型方法:public class Fanxing { public <T> ...
- API 管理在云原生场景下的机遇与挑战
作者 | 张添翼 来源 | 尔达Erda公众号 云原生下的机遇和挑战 标准和生态的意义 自从 Kubernetes v1.0 于 2015 年 7 月 21 日发布,CNCF 组织随后建立以来,其 ...
- Sharding-JDBC 实现垂直分库水平分表
1.需求分析
- Linux学习 - 权限管理命令
一.chmod(change the permissions mode of a file) 1 功能 改变文件或目录权限 root 与 所有者 可进行此操作 2 语法 chmod [(ugoa) ...
- list通过比较器进行排序
Collections.sort(dataList,new Comparator<BaseTransitData>(){ public int compare(Bas ...
- 使用AOP思想实现日志的添加
//1.创建日志表syslog------->创建日志的实体类--------->在web.xml中配置监听 <listener> <listener-class ...
- 【HarmonyOS】【多线程与并发】EventHandler
EventHandler与EventRunner EventHandler相关概念 ● EventHandler是一种用户在当前线程上投递InnerEvent事件或者Runnable任务到异步线程上处 ...
- 快速上手ANTLR
回顾前文: ANTLR 简单介绍 ANTLR 相关术语 ANTLR 环境准备 下面通过两个实例来快速上手ANTLR. 使用Listener转换数组 完整源码见:https://github.com/b ...