当前位置:网站首页>Daily question -890 Find and replace mode
Daily question -890 Find and replace mode
2022-06-12 16:22:00 【Programmed ape without hair loss 2】
subject :
You have a list of words words And a pattern pattern, Do you want to know words Which words in match the pattern .
If there is an arrangement of letters p , Make every letter in the pattern x Replace with p(x) after , We get the words we need , So the words match the patterns .
( Think about it , The arrangement of letters is from letter to letter : Each letter maps to another letter , No two letters map to the same letter .)
return words List of words matching the given pattern in .
You can return the answers in any order .
Example :
Input :words = [“abc”,“deq”,“mee”,“aqq”,“dkd”,“ccc”], pattern = “abb”
Output :[“mee”,“aqq”]
explain :
“mee” Match pattern , Because there are permutations {a -> m, b -> e, …}.
“ccc” Does not match pattern , because {a -> c, b -> c, …} It's not a permutation .
because a and b Map to the same letter .
Tips :
1 <= words.length <= 50
1 <= pattern.length = words[i].length <= 20
java Code :
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 The same letter in must be mapped to pattern On the same letter in
return false;
}
}
return true;
}
}
边栏推荐
- Batch --04--- moving components
- 思考游戏王决斗链接中抽卡概率问题
- <山东大学项目实训>渲染引擎系统(八-完)
- generate pivot data 0
- Read MHD and raw images, slice, normalize and save them
- 面试:hashCode()和equals()
- acwing 800. 数组元素的目标和
- Global and Chinese markets of bioreactors 2022-2028: Research Report on technology, participants, trends, market size and share
- 批量--04---移动构件
- <山东大学项目实训>渲染引擎系统(四)
猜你喜欢

Thinking about the probability of drawing cards in the duel link of game king

Acwing 797 differential

Office VR porn, coquettish operation! The father of Microsoft hololens resigns!

acwing796 子矩阵的和

acwing 803. Interval merging
![In 2020, the demand for strain sensors in China will reach 9.006 million, and the market scale will reach 2.292 billion yuan [figure]](/img/a8/dd5f79262fe6196dd44ba416a4baac.jpg)
In 2020, the demand for strain sensors in China will reach 9.006 million, and the market scale will reach 2.292 billion yuan [figure]

HEMA is the best representative of future retail

acwing788. 逆序对的数量

Unicom Network Management Protocol block diagram
![[tool recommendation] personal local markdown knowledge map software](/img/a2/5607df183ab37f32ae3fcd984d228e.png)
[tool recommendation] personal local markdown knowledge map software
随机推荐
Thinking about the probability of drawing cards in the duel link of game king
The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.7 实例(存储分配程序)
Global and Chinese market of soft capsule manufacturing equipment 2022-2028: Research Report on technology, participants, trends, market size and share
Huawei equipment is configured with CE dual attribution
Glibc memory management model frees C library memory cache
In 2020, the demand for strain sensors in China will reach 9.006 million, and the market scale will reach 2.292 billion yuan [figure]
Global and Chinese market for material injection 2022-2028: Research Report on technology, participants, trends, market size and share
34-【go】Golang channel知识点
Cookie 和 Session
深入理解 Go Modules 的 go.mod 與 go.sum
Interview: what are shallow copy and deep copy?
Global and Chinese market of vascular prostheses 2022-2028: Research Report on technology, participants, trends, market size and share
5-5配置Mysql复制 基于日志点的复制
generate pivot data 2
Global and Chinese market for commercial ceiling fans 2022-2028: Research Report on technology, participants, trends, market size and share
(四)GoogleNet複現
Analysis on the current situation of China's antiarrhythmic drug industry in 2021: domestic R & D is further [figure]
h t fad fdads
GloVe词嵌入(IMDB电影评论情感预测项目实战)
Scanpy (VI) analysis and visualization of spatial transcriptome data