-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
52 lines (51 loc) · 1.5 KB
/
Program.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
52
using System;
using System.Collections.Generic;
using System.Text;
namespace StringsDifferByOneCharacter
{
class Program
{
static void Main(string[] args)
{
string[] dict = { "abcd", "cccc", "abyd", "abab" };
Console.WriteLine(DifferByOne(dict));
}
static bool DifferByOne_HashSet(string[] dict)
{
var set = new HashSet<string>();
foreach (var word in dict)
{
var str = new StringBuilder(word);
for (int i = 0; i < str.Length; i++)
{
var letter = str[i];
str[i] = '*';
if (!set.Add(str.ToString()))
return true;
str[i] = letter;
}
}
return false;
}
static bool DifferByOne(string[] dict)
{
for (int i = 0; i < dict.Length; i++)
{
var word1 = dict[i];
for (int j = i + 1; j < dict.Length; j++)
{
var word2 = dict[j];
int count = 0;
for (int k = 0; k < word1.Length; k++)
{
if (word1[k] != word2[k])
count++;
if(count > 1) break;
}
if (count == 1) return true;
}
}
return false;
}
}
}