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. 如何处理Win10电脑黑屏后出现代码0xc0000225的错误?

    有些Win10系统的用户反映电脑在开机的时候突然变成黑屏,还出现提示0xc0000225的错误代码,不知道该怎么去解决.一般来说,遇到这种情况一般是系统的注册表出现了问题.下面就为大家分享一下相应的解 ...

  2. SQL 语句外键 a foreign key constraint fails

    queryRunner.update("SET FOREIGN_KEY_CHECKS = 0;"); queryRunner.update(sql, pid); queryRunn ...

  3. uvalive 5905 Pool construction

    题目链接 题意: 有一个花园,有些地方是草地,有些地方是洞,现在要在这个花园中修建一个泳池,泳池全部由洞构成. 把洞变成草地需要花费一定的费用,把草地变成洞也需要花费一定的费用,并且泳池的边缘的草地上 ...

  4. 在配置tensorflow时踩的无数个坑

    在下午尝试配置tensorflow环境时,遇到了许多天坑,讲真的心态炸了好几次,特此写下这篇记录,希望能给看到朋友一点帮助. 先说一下这抓狂的一天的起因,比赛项目想用SVM进行一下数据分析,除了常规的 ...

  5. 常用到用css3实现的转换,过渡和动画

    为什么要用css动画替换js动画 导致JavaScript效率低的两大原因:操作DOM和使用页面动画. 通常我们会通过频繁的操作 DOM的CSS来实现视觉上的动画效果,导致js效率低的两个因素都包括在 ...

  6. main方法类 为何由AppClassLoader加载

    AppClassLoader AppClassLoader应用类加载器,又称系统类加载器,负责在JVM启动时加载来自命令java中的classpath或者java.class.path系统属性或者CL ...

  7. 原生 JS实现一个简单分页插件

    最近做的一个 PC端的需求,这个需求中有一个小点,页面底部有一块列表区域,这个列表的数据量比较大,需要进行分页控制,切换页码的时候,发送一个 ajax请求,在页面无刷新的情况下,实现列表数据的刷新,所 ...

  8. 多个Promise执行顺序

    app.isLogin() // 判断是否登录后 .then(res=>{ this.setData({ login: true }, res2=>{ // 清空临时积分 return a ...

  9. python面试题--连续出现最大次数

    确实有段时间没怎么写python,手写还不上机是真的难受. 而且break 跳出循环最内一层的事情都要想一下才能写得出来. 题目如下: 寻找一个字符串最大连续出现次数,并放入字典中, s=" ...

  10. CodeForces 778B - Bitwise Formula

    题意: 选择一个 m 位的二进制数字,总分为 n 个算式的答案之和.问得到最低分和最高分分别应该取哪个二进制数字 分析: 因为所有数字都是m位的,高位的权重大于低位 ,我们就从高到低考虑 ans 的每 ...