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 ...
随机推荐
- 07-【jsp基本了解】
jsp 动态网页技术:服务器和用户交互的动态网页技术jsp[java server page ]jsp ≍ html +servletjsp 文件是以 *.jsp结尾:jsp文件 靠服务器运行,jsp ...
- linux 命令技巧(转)--history
本文介绍一些关于bash的能够提高效率的技巧,主要是关于历史命令操作和一些快捷键,让你在命令行下工作效率翻倍. 1.history-----最基本的查看历史命令 2.!n-----编号为n的历史命令 ...
- linux内核 概念
内核Kernel只是操作系统的一部分,操作系统本身在内核之上还包含命令行shell和其他种类的用户界面. 通常内核包含: 中断服务程序 进程管理和调度程序 内存管理程序 进程同步方法 IO和设备等 在 ...
- windows设置自动清理log
@echo off set srcDir="D:\xx\xx\xx" set daysAgo=5 forfiles /p %srcDir% /s /m *.* /d -%daysA ...
- 用Python调用Shell命令
Python经常被称作“胶水语言”,因为它能够轻易地操作其他程序,轻易地包装使用其他语言编写的库,也当然可以用Python调用Shell命令. 用Python调用Shell命令有如下几种方式: 第一种 ...
- springboot与springcloud版本不对应导致报错java.lang.NoSuchMethodError: org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava/lang/Object;)V
springboot启动报错: 10:31:50.221 [main] ERROR org.springframework.boot.SpringApplication - Application r ...
- 题解 最长上升序列2 — LIS2
最长上升序列2 - LIS2 Description 已知一个 1 ∼ N 的排列的最长上升子序列长度为 K ,求合法的排列个数. Input 输入一行二个整数 N , K ( K ≤ N ≤ 15) ...
- react创建项目后运行npm run eject命令将配置文件暴露出来时报错解决方法
最近在用create-react-app创建项目,因要配置各种组件,比如babel,antd等, 需要运行npm run eject命令把项目的配置文件暴露出来,但是还是一如既然碰到报错,因为是在本地 ...
- 对称加密算法DES、3DES和AES 原理总结(转载)
1.对称加密算法 1.1 定义 对称加密算法是应用较早的加密算法,技术成熟.在对称加密算法中,数据发信方将明文(原始数据)和加密密钥(mi yue)一起经过特殊加密算法处理后,使其变成复杂的加密密文发 ...
- CentOS下安装libjpeg库及编译GD库
GD库明明安装了,可处理图片的时候还是报错 Fatal error: Call to undefined function imagecreatefromjpeg() .PHP安装后,默认的gd库不支 ...