当前位置:网站首页>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 648(C#)
- 99% of users often make mistakes in power Bi cloud reports
- How to implement safety practice in software development stage
- How to mount the original data disk without damage after the reinstallation of proxmox ve?
- skimage学习(1)
- 如何在软件研发阶段落地安全实践
- Repair method of firewall system crash and file loss, material cost 0 yuan
- 麒麟信安加入宁夏商用密码协会
- LeetCode 1186. 删除一次得到子数组最大和 每日一题
- Matplotlib绘制三维图形
猜你喜欢

NeRF:DeepFake的最终替代者?

QT 图片背景色像素处理法

麒麟信安操作系统衍生产品解决方案 | 存储多路径管理系统,有效提高数据传输可靠性

AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究

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

麒麟信安中标国网新一代调度项目!

Biped robot controlled by Arduino
![[video / audio data processing] Shanghai daoning brings you elecard download, trial and tutorial](/img/14/4e7ebfb1ed5b99f8377af9d17d2177.jpg)
[video / audio data processing] Shanghai daoning brings you elecard download, trial and tutorial

Matplotlib绘制三维图形

With the latest Alibaba P7 technology system, mom doesn't have to worry about me looking for a job anymore
随机推荐
LeetCode刷题day49
Mrs offline data analysis: process OBS data through Flink job
DNS 系列(一):为什么更新了 DNS 记录不生效?
麒麟信安加入宁夏商用密码协会
LeetCode 1774. The dessert cost closest to the target price is one question per day
QT picture background color pixel processing method
Flask搭建api服务
Pycharm IDE下载
Skimage learning (2) -- RGB to grayscale, RGB to HSV, histogram matching
第二十四届中国科协湖南组委会调研课题组一行莅临麒麟信安调研考察
From Devops to mlops: how do it tools evolve to AI tools?
赋能智慧电力建设 | 麒麟信安高可用集群管理系统,保障用户关键业务连续性
LeetCode 1696. Jumping game VI daily question
skimage学习(1)
Sator a lancé le jeu web 3 "satorspace" et a lancé huobi
麒麟信安携异构融合云金融信创解决方案亮相第十五届湖南地区金融科技交流会
Sator推出Web3游戏“Satorspace” ,并上线Huobi
With the latest Alibaba P7 technology system, mom doesn't have to worry about me looking for a job anymore
《产品经理必读:五种经典的创新思维模型》的读后感
麒麟信安操作系统衍生产品解决方案 | 存储多路径管理系统,有效提高数据传输可靠性