【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 ...
随机推荐
- Markdown—.md文件是什么?怎么打开?
md全称markdown,markdown也是一种标记语言. md文件其实可以用常用的文本编辑器都可以打开. 用记事本打开,把markdown文件拖到记事本图标上就可以打开 . 用 subli ...
- Python压缩&解压缩
Python中常用的压缩模块有zipfile.tarfile.gzip 1.zipfile模块的简单使用 import zipfile # 压缩 z1 = zipfile.ZipFile('zip_t ...
- C++类虚函数内存分布(这个 你必须懂)
转自:http://www.cnblogs.com/jerry19880126/p/3616999.html C++类内存分布 书上类继承相关章节到这里就结束了,这里不妨说下C++内存分布结构,我们来 ...
- html5的canvas鼠标点击画圆
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- 【风控算法】一、变量分箱、WOE和IV值计算
一.变量分箱 变量分箱常见于逻辑回归评分卡的制作中,在入模前,需要对原始变量值通过分箱映射成woe值.举例来说,如"年龄"这一变量,我们需要找到合适的切分点,将连续的年龄打散到不同 ...
- A Child's History of England.1
A Child's History of England, by Charles Dickens (狄更斯) CHAPTER I ANCIENT ENGLAND AND THE ROMANS If y ...
- Linux 内存泄漏 valgrind
Valgrind 是个开源的工具,功能很多.例如检查内存泄漏工具---memcheck. Valgrind 安装: 去官网下载: http://valgrind.org/downloads/curre ...
- d3 CSS
CSS的inline.block与inline-block 块级元素(block):独占一行,对宽高的属性值生效:如果不给宽度,块级元素就默认为浏览器的宽度,即就是100%宽. 行内元素(inline ...
- 【Linux】【Commands】文本查看类
分屏查看命令:more和less more命令: more FILE 特点:翻屏至文件尾部后自动退出: less命令: less FILE head命令: 查看文件的前n行: head [option ...
- Pycharm, 添加requirements.txt
引用:https://www.jetbrains.com/help/pycharm/managing-dependencies.html 1) 2) 3)