当前位置:网站首页>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;
}
}
边栏推荐
- Thread factory in thread pool
- ip netns 命令(备忘)
- 2022年推荐免费在线接收短信平台(国内、国外)
- 线程池和单例模式以及文件操作
- Reject policy of thread pool
- 【软件测试】从企业版BOSS直聘,看求职简历,你没被面上是有原因的
- The live broadcast reservation channel is open! Unlock the secret of fast launching of audio and video applications
- Idea completely uninstalls installation and configuration notes
- 体总:安全有序恢复线下体育赛事,力争做到国内赛事应办尽办
- Redis publishing and subscription
猜你喜欢
[paper sharing] where's crypto?
Save the memory of the model! Meta & UC Berkeley proposed memvit. The modeling time support is 30 times longer than the existing model, and the calculation amount is only increased by 4.5%
线程池和单例模式以及文件操作
Static routing configuration
SlashData开发者工具榜首等你而定!!!
如何选择合适的自动化测试工具?
Redis集群与扩展
伺服力矩控制模式下的力矩目标值(fTorque)计算
DeSci:去中心化科学是Web3.0的新趋势?
我感觉被骗了,微信内测 “大小号” 功能,同一手机号可注册两个微信
随机推荐
How many times is PTA 1101 B than a
In 2021, the national average salary was released. Have you reached the standard?
Embedded interview questions (algorithm part)
AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究
Usage of PHP interview questions foreach ($arr as $value) and foreach ($arr as $value)
Learn open62541 -- [67] add custom enum and display name
10 schemes to ensure interface data security
idea彻底卸载安装及配置笔记
清华、剑桥、UIC联合推出首个中文事实核查数据集:基于证据、涵盖医疗社会等多个领域
Sports Federation: resume offline sports events in a safe and orderly manner, and strive to do everything possible for domestic events
Redis集群与扩展
【Base64笔记】「建议收藏」
Reject policy of thread pool
C语言中匿名的最高境界
SlashData开发者工具榜首等你而定!!!
6. About JWT
[sword finger offer] 59 - I. maximum value of sliding window
Will low code help enterprises' digital transformation make programmers unemployed?
App capture of charles+postern
How to implement safety practice in software development stage