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的更多相关文章

  1. 890. Find and Replace Pattern - LeetCode

    Question 890. Find and Replace Pattern Solution 题目大意:从字符串数组中找到类型匹配的如xyy,xxx 思路: 举例:words = ["ab ...

  2. 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 ...

  3. [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 ...

  4. Leetcode 890. Find and Replace Pattern

    把pattern映射到数字,也就是把pattern标准化. 比如abb和cdd如果都能标准化为011,那么就是同构的. class Solution: def findAndReplacePatter ...

  5. 【LeetCode】890. Find and Replace Pattern 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+set 单字典 日期 题目地址:https:/ ...

  6. [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 ...

  7. LC 833. Find And Replace in String

    To some string S, we will perform some replacement operations that replace groups of letters with ne ...

  8. 【ARTS】01_16_左耳听风-20190225~20190303

    ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. SpringMVC----视图层框架

    Spring Web模型-视图-控制器(MVC)框架是围绕DispatcherServlet设计的,DispatcherServlet将接收的请求分派给应用程序.SpringMVC具有配置处理程序映射 ...

  2. fastadmin tp等模板变量在html中的js中如何使用?

    <script> var a = {$b} </script>

  3. 第八章· Redis API 开发

    Redis 开发 1.源码安装Python环境 Python官网:https://www.python.org/ #下载Python3.6.4安装包 [root@db03 ~]# wget https ...

  4. 第十五章·Kibana深入-Dev Tools及Lucene语法

    Dev Tools介绍 Dev Tools 页面包含开发工具,您可以使用这些Dev Tools与Kibana中的数据进行交互. 原先的交互式控制台Sense,使用户方便的通过浏览器直接与Elastic ...

  5. Linux用户组管理及用户权限4

    权限管理:    ls -l        rwxrwxrwx:            左三位:定义user(owner)的权限            中三位:定义group的权限           ...

  6. CNN for NLP

    卷积神经网络在自然语言处理任务中的应用.参考链接:Understanding Convolutional Neural Networks for NLP(2015.11) Instead of ima ...

  7. Django :执行 python manage.py makemigrations 时报错 TypeError: __init__() missing 1 required positional argument: 'on_delete'

    原因 执行命令 python manage.py makemigrations 报错 TypeError: __init__() missing 1 required positional argum ...

  8. strace分析启动流程

    strace -ttt /usr/local/mysql/bin/mysqld 2>&1 |tee 1.log vi 1.log 1 1530183737.761709 execve(& ...

  9. Ribbon整合Consul,出现No instances available for XXX 异常

    先贴代码: @RestController public class CallHelloController { @Autowired private LoadBalancerClient loadB ...

  10. 浏览器表单自动填充默认样式 - autofill问题解决

    这不知道是个什么神仙网站,解决办法写的这么清楚,页面有这么让人舒服 https://lhajh.github.io/css/chrome/2018/04/17/The-chrome-browser-f ...