当前位置:网站首页>LeetCode 890(C#)
LeetCode 890(C#)
2022-07-07 19:05:00 【Just be interesting】
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 .
Code
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;
}
}
边栏推荐
猜你喜欢
gsap动画库
【塔望方法论】塔望3W消费战略 - U&A研究法
高温火烧浑不怕,钟薛高想留清白在人间
如何给“不卖笔”的晨光估值?
[unity shader] insert pass to realize the X-ray perspective effect of model occlusion
The performance and efficiency of the model that can do three segmentation tasks at the same time is better than maskformer! Meta & UIUC proposes a general segmentation model with better performance t
Do you know all four common cache modes?
99% of people don't know that privatized deployment is also a permanently free instant messaging software!
低代码助力企业数字化转型会让程序员失业?
Redis cluster and expansion
随机推荐
Golang client server login
6. About JWT
Skills of embedded C language program debugging and macro use
ip netns 命令(备忘)
2022上半年朋友圈都在传的10本书,找到了
【Unity Shader】插入Pass实现模型遮挡X光透视效果
[unity shader] insert pass to realize the X-ray perspective effect of model occlusion
The highest level of anonymity in C language
Yunjing network technology interview question [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
Thread pool and singleton mode and file operation
[Blue Bridge Cup training 100 questions] sort scratch from small to large. Blue Bridge Cup scratch competition special prediction programming question centralized training simulation exercise question
强化学习-学习笔记8 | Q-learning
SQLite SQL exception near "with": syntax error
行业案例|数字化经营底座助力寿险行业转型
sqlite sql 异常 near “with“: syntax error
POJ 2392 Space Elevator
嵌入式面试题(算法部分)
Charles+drony的APP抓包
How much does it cost to develop a small program mall?
NAT地址转换