-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution676.cs
51 lines (44 loc) · 1.25 KB
/
Solution676.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
42
43
44
45
46
47
48
49
50
51
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution676
{
/// <summary>
/// 676. Implement Magic Dictionary - Medium
/// <a href="https://leetcode.com/problems/implement-magic-dictionary">See the problem</a>
/// </summary>
public class MagicDictionary
{
private readonly HashSet<string> dict = [];
public void BuildDict(string[] dictionary)
{
foreach (var word in dictionary)
{
dict.Add(word);
}
}
public bool Search(string searchWord)
{
foreach (var word in dict)
{
if (word.Length == searchWord.Length && IsOneCharDiff(word, searchWord))
{
return true;
}
}
return false;
}
private static bool IsOneCharDiff(string word, string searchWord)
{
int diffCount = 0;
for (int i = 0; i < word.Length; i++)
{
if (word[i] != searchWord[i])
{
diffCount++;
if (diffCount > 1) return false;
}
}
return diffCount == 1;
}
}
}