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 <= 50
1 <= 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 ...
随机推荐
- JavaMaven【七、插件使用】
配置pom.xml 配置在那个周期的那个阶段执行该插件的功能 上图是配置了使用插件source3.0.1,该插件的功能是打包源码 并配置了在package阶段后执行打包源码的操作jar-no-fork ...
- 解决myeclipse没有代码提示的问题
今天和室友安装了一样的myeclipse版本,结果室友的自动提示功能有,我的输入“.”后却不能提示,这对我们敲代码简直来说是一个折磨,不能自动提示,本来还以为是系统问题,一个是win7,一个是win1 ...
- 文件浏览:ls&file
文件浏览主要完成的功能是列出当前目录或指定目录下文件的文件名称以及一些基本的资料. Linux的命令是区分大小写的. 1. ls:列出当前目录(默认)或指定目录下所有文件,并按字典序排序 语法:ls ...
- QT对话框
QFileDialog:文件对话框 QString fileName=QFileDialog::getOpenFileName(this,"打开文件", "/" ...
- Git使用教程学习
Git使用教程学习 在第十二周的个人作业上,王文娟老师希望我们去自己课后了解一下git的使用方式以及一些基础知识,在本学期其他的课程上,我们已经稍微了解过一些git的基础知识,因此在本次作业里,我补充 ...
- Java 包装类及其与String转换、进制转换
一.包装类 1.基本类型和引用类型 Java中的基本类型我们都知道有8种,但是作为基本类型限制功能的发挥,例如整形转String类型等可能需要类方法实现会更加简便.那么八个基本类型对应八个包装类,即引 ...
- 更改centos的网卡名
Centos6更改网卡名的方法: 1.修改皮配置文件/etc/udev/rules.d/70-persistent-net.rules # This file was automatically ge ...
- shell的正则表达式
正则表达式处理文件的内容,shell处理文件本身 grep *匹配0到n个 .(点儿)能匹配任意字符----8.8.8.8用于测试外网是否通畅 egrep
- [学习笔记] 平衡树——Treap
前置技能:平衡树前传:BST 终于学到我们喜闻乐见的平衡树啦! 所以我们这次讲的是平衡树中比较好写的\(Treap\). (以后会写splay的先埋个坑在这) 好了,进入正题. step 1 我们知道 ...
- js 获取 touch length
window.addEventListener("touchstart", touchHandler, false); function touchHandler(event){ ...