当前位置:网站首页>每日一题-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;
}
}
边栏推荐
- Step by step to create a trial version of ABAP program containing custom screen
- Chapter I linear table
- 当编程纳入到高考。。。
- Keep an IT training diary 067- good people are grateful, bad people are right
- acwing 798二维差分(差分矩阵)
- Interview: hashcode() and equals()
- Redis string type common commands
- 5-5 configuring MySQL replication log point based replication
- go net库(待续)
- Analysis on the current situation of China's antiarrhythmic drug industry in 2021: domestic R & D is further [figure]
猜你喜欢

批量--03---CmdUtil

Thread pool execution process

RTOS rt-thread裸机系统与多线程系统

Solution to idea Chinese prism garbled code error -- console Chinese output prism garbled code

Homology? Cross domain? How to solve cross domain problems?

acwing788. 逆序对的数量
![Analysis of global and Chinese shipbuilding market in 2021: the global shipbuilding new orders reached 119.85 million dwt, with China, Japan and South Korea accounting for 96.58%[figure]](/img/3e/b54b7f15c4a6326d8c7c4433388a3a.jpg)
Analysis of global and Chinese shipbuilding market in 2021: the global shipbuilding new orders reached 119.85 million dwt, with China, Japan and South Korea accounting for 96.58%[figure]

acwing 801. 二进制中1的个数(位运算)

< 山东大学软件学院项目实训 > 渲染引擎系统——辐射预计算(八)
![In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]](/img/dd/1bf44d284c709b6bebd4b308ba2cee.jpg)
In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]
随机推荐
统计机器学习代码合集
MySQL blob and text types
Unicom Network Management Protocol block diagram
Solution to idea Chinese prism garbled code error -- console Chinese output prism garbled code
acwing 802. Interval sum (discretization)
Homology? Cross domain? How to solve cross domain problems?
HEMA is the best representative of future retail
When programming is included in the college entrance examination...
Development status of China's pig breeding industry in 2021 and comparative analysis of key enterprises: 671million pigs were sold [figure]
Project training of Software College of Shandong University rendering engine system basic renderer (6)
批量--03---CmdUtil
Analysis of global and Chinese shipbuilding market in 2021: the global shipbuilding new orders reached 119.85 million dwt, with China, Japan and South Korea accounting for 96.58%[figure]
Global and Chinese markets of automatic glue applicators 2022-2028: Research Report on technology, participants, trends, market size and share
看《梦华录》上头的人都该尝试下这款抖音特效
Keep an IT training diary 067- good people are grateful, bad people are right
聊聊事件监听那些事-上
acwing795 前缀和(一维)
[tool recommendation] personal local markdown knowledge map software
Analysis of China's cargo transport volume, cargo transport turnover and port cargo in 2021 [figure]
[browser principle] variable promotion