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 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
Runtime: 4 ms, faster than 62.45% of C++ online submissions for Find and Replace Pattern.
注意一一对应要用两个字典。
class Solution {
public:
vector<string> findAndReplacePattern(vector<string>& words, string pattern) {
vector<string> ret;
for(auto word : words){
if(word.size() != pattern.size()) continue;
unordered_map<char,char> mp_w2p;
unordered_map<char,char> mp_p2w;
bool shouldput = true;
for(int i=; i<word.size(); i++){
if(!mp_w2p.count(word[i]) && !mp_p2w.count(pattern[i])){
mp_w2p[word[i]] = pattern[i];
mp_p2w[pattern[i]] = word[i];
} else if(!mp_w2p.count(word[i]) || !mp_p2w.count(pattern[i])) {
shouldput = false;
break;
}else if(mp_w2p[word[i]] != pattern[i] || mp_p2w[pattern[i]] != word[i]){
shouldput = false;
break;
}
}
if(shouldput) ret.push_back(word);
}
return ret;
}
};
LC 890. Find and Replace Pattern的更多相关文章
- 890. Find and Replace Pattern - LeetCode
Question 890. Find and Replace Pattern Solution 题目大意:从字符串数组中找到类型匹配的如xyy,xxx 思路: 举例:words = ["ab ...
- 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] 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
把pattern映射到数字,也就是把pattern标准化. 比如abb和cdd如果都能标准化为011,那么就是同构的. class Solution: def findAndReplacePatter ...
- 【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 ...
- LC 833. Find And Replace in String
To some string S, we will perform some replacement operations that replace groups of letters with ne ...
- 【ARTS】01_16_左耳听风-20190225~20190303
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- 第四章·Kibana入门-安装,索引添加及界面功能
1.Kibana简介及部署 什么是Kibana? Kibana是一个通过调用elasticsearch服务器进行图形化展示搜索结果的开源项目. Kibana安装及配置 #将Kibana安装包上传至服务 ...
- Cowrie蜜罐部署教程
0.蜜罐分类: 低交互:模拟服务和漏洞以便收集信息和恶意软件,但是攻击者无法和该系统进行交互: 中等交互:在一个特有的控制环境中模拟一个生产服务,允许攻击者的部分交互: 高交互:攻击者可以几乎自由 ...
- 【转】container_of宏 分析
在学习Linux驱动的过程中,遇到一个宏叫做container_of.该宏定义在include/linux/kernel.h中,首先来贴出它的代码: /** * container_of - cast ...
- deep_learning_backprop
反向传播理解–从抽象到具体 神经网络从计算的角度看,数据是从底层输入,经过每一层,根据与该层之间的权重计算以一个中间结果,这个中间结果再经过一个非线性激活函数作用,得到该层的输出结果,然后把该层的输出 ...
- libusb_transfer
http://libusb.sourceforge.net/api-1.0/structlibusb__transfer.html int libusb_control_transfer(libusb ...
- springboot2.1.7整合Druid
一.maven的依赖:文中就贴重点的, 其他依赖就不贴了 <dependency> <groupId>com.alibaba</groupId> <artif ...
- Linux的信号管理
man 7 signal #查看信号的实用信息 常用的信号: 信号名 编号 含义SIGHUP 1 无须关闭进程而让其重读配置文件SIGINT 2 ...
- 由函数$y=\sin x$的图像伸缩变换为函数$y=\sin(\omega x)$的图像(交互式)
可以拖动滑动条\(\omega\)显示动态效果
- 记录weblogic12c通用版本在windowsXP上安装报异常的问题:java.lang.NoClassDefFoundError:
先上图: 这个可能有经验的人一眼就看出问题了,报这个错的原因是:我把安装包放在了中文目录下,本来以为安装包放在中文目录下无关紧要,但是weblogic12c就是这么神奇,不能放在中文目录下. 解决方法 ...
- freemarker 生成word
一.生成模板,动态获取的部分用${变量名},然后将word另存为xml文件,再将后缀名改成ftl格式.然后将模板放在对应的目录下. 二.引入freemarker包,mawen引用 <depend ...