当前位置:网站首页>LeetCode 890(C#)
LeetCode 890(C#)
2022-07-07 15:38:00 【有趣就行】
题目
你有一个单词列表 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 映射到同一个字母。
代码
public class Solution
{
public IList<string> FindAndReplacePattern(string[] words, string pattern)
{
return words.Where(a => Match(a, pattern) && Match(pattern, a)).ToList();
}
private bool Match(string a, string pattern)
{
if (a.Length != pattern.Length) return false;
Dictionary<char, char> dic = new Dictionary<char, char>();
for (int i = 0; i < pattern.Length; i++)
{
(char x, char y) = (pattern[i], a[i]);
if (!dic.ContainsKey(x)) dic.Add(x, y);
else if (dic[x] != y) return false;
}
return true;
}
}
边栏推荐
- LeetCode 1696. 跳跃游戏 VI 每日一题
- [fan Tan] after the arrival of Web3.0, where should testers go? (ten predictions and suggestions)
- mysql实现两个字段合并成一个字段查询
- LeetCode 1626. 无矛盾的最佳球队 每日一题
- QT视频传输
- 【Seaborn】组合图表、多子图的实现
- rpcms获取指定分类下的文章的方法
- LeetCode 1477. 找两个和为目标值且不重叠的子数组 每日一题
- [Huang ah code] Why do I suggest you choose go instead of PHP?
- LeetCode 1049. 最后一块石头的重量 II 每日一题
猜你喜欢

The top of slashdata developer tool is up to you!!!

Mrs offline data analysis: process OBS data through Flink job

PLC: automatically correct the data set noise, wash the data set | ICLR 2021 spotlight

科普达人丨一文弄懂什么是云计算?

Sator推出Web3遊戲“Satorspace” ,並上線Huobi

Pychart ide Download

DevOps 的运营和商业利益指南

QT picture background color pixel processing method

The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars

Is AI more fair than people in the distribution of wealth? Research on multiplayer game from deepmind
随机推荐
LeetCode 1696. Jumping game VI daily question
LeetCode 312. Poke balloon daily
QML beginner
从DevOps到MLOps:IT工具怎样向AI工具进化?
LeetCode 1654. 到家的最少跳跃次数 每日一题
【黄啊码】为什么我建议您选择go,而不选择php?
LeetCode 1626. 无矛盾的最佳球队 每日一题
Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls
Problems encountered in Jenkins' release of H5 developed by uniapp
Flask build API service SQL configuration file
DNS series (I): why does the updated DNS record not take effect?
Jenkins发布uniapp开发的H5遇到的问题
Skimage learning (1)
LeetCode 1774. The dessert cost closest to the target price is one question per day
QT picture background color pixel processing method
【视频/音频数据处理】上海道宁为您带来Elecard下载、试用、教程
Flask搭建api服务-SQL配置文件
自定义View必备知识,Android研发岗必问30+道高级面试题
Sator a lancé le jeu web 3 "satorspace" et a lancé huobi
Test case management tool recommendation