当前位置:网站首页>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;
}
}
边栏推荐
- 2022-07-04 matlab读取视频帧并保存
- go语言的字符串类型、常量类型和容器类型
- [C language] string function
- 基于图像和激光的多模态点云融合与视觉定位
- Kirk Borne的本周学习资源精选【点击标题直接下载】
- [demo] circular queue and conditional lock realize the communication between goroutines
- PTA 1102 教超冠军卷
- Standard ACL and extended ACL
- 我感觉被骗了,微信内测 “大小号” 功能,同一手机号可注册两个微信
- Complete e-commerce system
猜你喜欢
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%
Multimodal point cloud fusion and visual location based on image and laser
Continuous test (CT) practical experience sharing
Skills of embedded C language program debugging and macro use
如何选择合适的自动化测试工具?
如何给“不卖笔”的晨光估值?
Scientists have observed for the first time that the "electron vortex" helps to design more efficient electronic products
The highest level of anonymity in C language
In 2021, the national average salary was released. Have you reached the standard?
直播预约通道开启!解锁音视频应用快速上线的秘诀
随机推荐
手把手教姐姐写消息队列
2022上半年朋友圈都在传的10本书,找到了
AntiSamy:防 XSS 攻击的一种解决方案使用教程
CVPR 2022 - learning non target knowledge for semantic segmentation of small samples
线程池的拒绝策略
Learn open62541 -- [67] add custom enum and display name
Mathematical analysis_ Notes_ Chapter 11: Fourier series
数据验证框架 Apache BVal 再使用
Rules for filling in volunteers for college entrance examination
How to implement safety practice in software development stage
Do you really understand sticky bag and half bag? 3 minutes to understand it
【软件测试】从企业版BOSS直聘,看求职简历,你没被面上是有原因的
Basic operation of chain binary tree (implemented in C language)
高考填志愿规则
Differences between rip and OSPF and configuration commands
Kubernetes DevOps CD工具对比选型
[information security laws and regulations] review
Improve application security through nonce field of play integrity API
如何选择合适的自动化测试工具?
Cloud security daily 220707: Cisco Expressway series and telepresence video communication server have found remote attack vulnerabilities and need to be upgraded as soon as possible