当前位置:网站首页>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 213. Home raiding II daily question
- 【饭谈】Web3.0到来后,测试人员该何去何从?(十条预言和建议)
- LeetCode 120. 三角形最小路径和 每日一题
- LeetCode 403. 青蛙过河 每日一题
- 赋能智慧电力建设 | 麒麟信安高可用集群管理系统,保障用户关键业务连续性
- LeetCode 300. Daily question of the longest increasing subsequence
- 【Seaborn】组合图表、多子图的实现
- First in China! Todesk integrates RTC technology into remote desktop, with clearer image quality and smoother operation
- LeetCode 152. Product maximum subarray daily question
猜你喜欢
How to add aplayer music player in blog
SlashData开发者工具榜首等你而定!!!
What is cloud computing?
QT picture background color pixel processing method
QML初学
The top of slashdata developer tool is up to you!!!
Skimage learning (2) -- RGB to grayscale, RGB to HSV, histogram matching
管理VDI的几个最佳实践
Matplotlib绘制三维图形
测试用例管理工具推荐
随机推荐
LeetCode 1043. 分隔数组以得到最大和 每日一题
Rpcms method of obtaining articles under the specified classification
LeetCode 1477. Find two subarrays with sum as the target value and no overlap
【Seaborn】组合图表、多子图的实现
Establishment of solid development environment
Blue Bridge Cup final XOR conversion 100 points
Pycharm IDE下载
QT picture background color pixel processing method
LeetCode 1774. The dessert cost closest to the target price is one question per day
Mrs offline data analysis: process OBS data through Flink job
Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls
The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars
mysql实现两个字段合并成一个字段查询
Flask搭建api服务-生成API文档
麒麟信安操作系统衍生产品解决方案 | 存储多路径管理系统,有效提高数据传输可靠性
Jenkins发布uniapp开发的H5遇到的问题
【饭谈】那些看似为公司着想,实际却很自私的故事 (一:造轮子)
LeetCode 1186. 删除一次得到子数组最大和 每日一题
User defined view essential knowledge, Android R & D post must ask 30+ advanced interview questions
rpcms获取指定分类下的文章的方法