当前位置:网站首页>每日一题-890. 查找和替换模式
每日一题-890. 查找和替换模式
2022-06-12 16:16:00 【程序猿不脱发2】
题目:
你有一个单词列表 words 和一个模式 pattern,你想知道 words 中的哪些单词与模式匹配。
如果存在字母的排列 p ,使得将模式中的每个字母 x 替换为 p(x) 之后,我们就得到了所需的单词,那么单词与模式是匹配的。
(回想一下,字母的排列是从字母到字母的双射:每个字母映射到另一个字母,没有两个字母映射到同一个字母。)
返回 words 中与给定模式匹配的单词列表。
你可以按任何顺序返回答案。
示例:
输入:words = [“abc”,“deq”,“mee”,“aqq”,“dkd”,“ccc”], pattern = “abb”
输出:[“mee”,“aqq”]
解释:
“mee” 与模式匹配,因为存在排列 {a -> m, b -> e, …}。
“ccc” 与模式不匹配,因为 {a -> c, b -> c, …} 不是排列。
因为 a 和 b 映射到同一个字母。
提示:
1 <= words.length <= 50
1 <= pattern.length = words[i].length <= 20
java代码:
class Solution {
public List<String> findAndReplacePattern(String[] words, String pattern) {
List<String> ans = new ArrayList<String>();
for (String word : words) {
if (match(word, pattern) && match(pattern, word)) {
ans.add(word);
}
}
return ans;
}
public boolean match(String word, String pattern) {
Map<Character, Character> map = new HashMap<Character, Character>();
for (int i = 0; i < word.length(); ++i) {
char x = word.charAt(i), y = pattern.charAt(i);
if (!map.containsKey(x)) {
map.put(x, y);
} else if (map.get(x) != y) { // word 中的同一字母必须映射到 pattern 中的同一字母上
return false;
}
}
return true;
}
}
边栏推荐
- acwing788. 逆序对的数量
- Global and Chinese market of medical ECG telemetry equipment 2022-2028: Research Report on technology, participants, trends, market size and share
- From K-means to capsule
- d的sha6转大整
- UE4 common type conversion
- Axure RP 9 for MAC (interactive product prototyping tool) Chinese version
- 办公室VR黄片,骚操作!微软HoloLens之父辞职!
- Sum of acwing796 submatrix
- 一步步创建包含自定义 Screen 的 ABAP 程序的详细步骤
- [weekly replay] game 80 of leetcode
猜你喜欢

The small flying page is upgraded to be intelligent and the bug repair is faster

思考游戏王决斗链接中抽卡概率问题

Read MHD and raw images, slice, normalize and save them

< 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(八)
![Analysis of China's cargo transport volume, cargo transport turnover and port cargo in 2021 [figure]](/img/ee/ae9984355a98f5529dd4574eea2e7c.jpg)
Analysis of China's cargo transport volume, cargo transport turnover and port cargo in 2021 [figure]
![[tool recommendation] personal local markdown knowledge map software](/img/a2/5607df183ab37f32ae3fcd984d228e.png)
[tool recommendation] personal local markdown knowledge map software

acwing 798二维差分(差分矩阵)

Axure RP 9 for MAC (interactive product prototyping tool) Chinese version

< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(五)

C packing and unpacking
随机推荐
RTOS RT thread bare metal system and multi thread system
In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]
Let's talk about events. Listen to those things. - Part one
2022.02.28 - SX11-05. The largest rectangle in the histogram
关于组件传值
acwing 802. 区间和 (离散化)
< 山东大学软件学院项目实训 > 渲染引擎系统——基础渲染器(七)
Browsercontext class of puppeter
面试:hashCode()和equals()
Read MHD and raw images, slice, normalize and save them
盒马,最能代表未来的零售
UE4 common type conversion
Sum of acwing796 submatrix
acwing 797 差分
acwing 800. 数组元素的目标和
puppeteer入门之 BrowserContext 类
看《梦华录》上头的人都该尝试下这款抖音特效
Project training of Software College of Shandong University rendering engine system basic renderer (6)
Project training of Software College of Shandong University rendering engine system radiation pre calculation (IX)
Global and Chinese markets of bioreactors 2022-2028: Research Report on technology, participants, trends, market size and share