-
Notifications
You must be signed in to change notification settings - Fork 115
/
0890-FindAndReplacePattern.cs
41 lines (35 loc) · 1.24 KB
/
0890-FindAndReplacePattern.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//-----------------------------------------------------------------------------
// Runtime: 240ms
// Memory Usage: 32 MB
// Link: https://leetcode.com/submissions/detail/360796985/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0890_FindAndReplacePattern
{
public IList<string> FindAndReplacePattern(string[] words, string pattern)
{
var result = new List<string>();
foreach (var word in words)
if (Match(word, pattern))
result.Add(word);
return result;
}
private bool Match(string word, string pattern)
{
var map1 = new Dictionary<char, char>();
var map2 = new Dictionary<char, char>();
for (int i = 0; i < pattern.Length; i++)
{
if (!map1.ContainsKey(pattern[i]))
map1[pattern[i]] = word[i];
if (!map2.ContainsKey(word[i]))
map2[word[i]] = pattern[i];
if (map1[pattern[i]] != word[i] || map2[word[i]] != pattern[i])
return false;
}
return true;
}
}
}