当前位置:网站首页>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;
}
}
边栏推荐
- SD_ DATA_ RECEIVE_ SHIFT_ REGISTER
- Do you know all four common cache modes?
- Short selling, overprinting and stock keeping, Oriental selection actually sold 2.66 million books in Tiktok in one month
- SQLite SQL exception near "with": syntax error
- 体总:安全有序恢复线下体育赛事,力争做到国内赛事应办尽办
- Wireshark分析抓包数据*.cap
- Improve application security through nonce field of play integrity API
- coming! Gaussdb (for Cassandra) new features appear
- Nunjuks template engine
- 10 schemes to ensure interface data security
猜你喜欢
A hodgepodge of ICER knowledge points (attached with a large number of topics, which are constantly being updated)
DataSimba推出微信小程序,DataNuza接受全场景考验? | StartDT Hackathon
数据验证框架 Apache BVal 再使用
Basic operation of chain binary tree (implemented in C language)
Nunjuks template engine
Charles+drony的APP抓包
Reuse of data validation framework Apache bval
如何给“不卖笔”的晨光估值?
App capture of charles+postern
Thread pool and singleton mode and file operation
随机推荐
In 2021, the national average salary was released. Have you reached the standard?
[paper sharing] where's crypto?
Continuous test (CT) practical experience sharing
10 schemes to ensure interface data security
RISCV64
Static routing configuration
Rules for filling in volunteers for college entrance examination
高温火烧浑不怕,钟薛高想留清白在人间
3. About cookies
SD_ DATA_ SEND_ SHIFT_ REGISTER
脑洞从何而来?加州大学最新研究:有创造力的人神经连接会「抄近道」
Kubernetes DevOps CD工具对比选型
Embedded interview questions (algorithm part)
C语言中匿名的最高境界
Industry case | digital operation base helps the transformation of life insurance industry
NAT地址转换
低代码助力企业数字化转型会让程序员失业?
[information security laws and regulations] review
国内的软件测试会受到偏见吗
持续测试(CT)实战经验分享