当前位置:网站首页>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 1654. 到家的最少跳跃次数 每日一题
- LeetCode 1155. N ways to roll dice one question per day
- 无法链接远程redis服务器(解决办法百分百)
- Solidity 开发环境搭建
- Share the latest high-frequency Android interview questions, and take you to explore the Android event distribution mechanism
- 【视频/音频数据处理】上海道宁为您带来Elecard下载、试用、教程
- LeetCode 1981. Minimize the difference between the target value and the selected element one question per day
- LeetCode 1155. 掷骰子的N种方法 每日一题
- LeetCode 648(C#)
- QML初学
猜你喜欢

SlashData开发者工具榜首等你而定!!!

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

skimage学习(3)——使灰度滤镜适应 RGB 图像、免疫组化染色分离颜色、过滤区域最大值

mysql官网下载:Linux的mysql8.x版本(图文详解)

Sator推出Web3游戏“Satorspace” ,并上线Huobi

专精特新软件开发类企业实力指数发布,麒麟信安荣誉登榜

Sator launched Web3 game "satorspace" and launched hoobi

测试用例管理工具推荐

Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images

Test case management tool recommendation
随机推荐
Is AI more fair than people in the distribution of wealth? Research on multiplayer game from deepmind
DNS 系列(一):为什么更新了 DNS 记录不生效?
DNS series (I): why does the updated DNS record not take effect?
麒麟信安云平台全新升级!
Sator推出Web3游戏“Satorspace” ,并上线Huobi
centos7安装mysql笔记
On Apache Doris Fe processing query SQL source code analysis
LeetCode 1186. Delete once to get the sub array maximum and daily question
自定义View必备知识,Android研发岗必问30+道高级面试题
Notes on installing MySQL in centos7
The top of slashdata developer tool is up to you!!!
LeetCode 403. 青蛙过河 每日一题
《产品经理必读:五种经典的创新思维模型》的读后感
[source code interpretation] | source code interpretation of livelistenerbus
How to add aplayer music player in blog
The mail server is listed in the blacklist. How to unblock it quickly?
防火墙系统崩溃、文件丢失的修复方法,材料成本0元
无法链接远程redis服务器(解决办法百分百)
电脑无法加域,ping域名显示为公网IP,这是什么问题?怎么解决?
LeetCode 213. Home raiding II daily question