-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordLink.cs
213 lines (182 loc) · 7.27 KB
/
WordLink.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace SeedFinder
{
public class WordLink
{
/// <summary>List of words from a dictionary.</summary>
public HashSet<string> WordList { get; private set; }
/// <summary>a,b, ... z</summary>
private List<char> az;
/// <summary>Constructs a WordLinker class to find paths between 2 words.</summary>
/// <param name="wordlistFilename">The full file path to the wordlist to use.</param>
/// <param name="changeableWordSize">Can the words grow and shrink as you attempt to get to the target word?</param>
public WordLink(string wordlistFilename, bool changeableWordSize)
{
this.changeableWordSize = changeableWordSize;
LoadDictionary(wordlistFilename);
LoadCharAZ();
}
/// <summary>Finds a link between 2 words changing 1 char at a time. NULL if no link found.</summary>
public List<string> Find(string start, string target)
{
if (!WordList.Contains(start) || !WordList.Contains(target))
return null;
// Breadth first search using a queue.
// The stack is used to keep track of our walk from start to target
Queue<Stack<string>> q = new Queue<Stack<string>>();
Stack<string> firstStep = new Stack<string>();
firstStep.Push(start);
q.Enqueue(firstStep);
while (q.Count > 0)
{
Stack<string> currentWalk = q.Dequeue();
string currentWord = currentWalk.Peek();
foreach (string nextWord in GetAdjacentNodes(currentWord))
{
if (currentWalk.Contains(nextWord))
continue; // already used this word, don't want to walk backwards
bool addNewWalk = true;
foreach (Stack<string> otherWalk in q)
{
if (otherWalk.Contains(nextWord))
{
// there is another walk, otherWalk, who's length is <= to this walk that gets to the same word.
// No need to pursue this walk.
addNewWalk = false;
break;
}
}
Stack<string> newWalk = new Stack<string>(currentWalk.Reverse());
newWalk.Push(nextWord);
if (nextWord == target)
{
// happy days
return new List<string>(newWalk.Reverse());
}
if (addNewWalk)
{
q.Enqueue(newWalk);
}
}
}
return null;
}
/// <summary>A dictionary of params->result for the method OneStep.</summary>
private ConcurrentDictionary<string, List<string>> _oneStepMemoization = new ConcurrentDictionary<string, List<string>>();
/// <summary>Can the word size grow/shrink by 1 character?</summary>
private bool changeableWordSize;
/// <summary>Returns all the valid words that are 1 edge away from the word given.</summary>
private IEnumerable<string> GetAdjacentNodes(string word)
{
List<string> result = null;
if (_oneStepMemoization.TryGetValue(word, out result))
{
return result;
}
result = Onestep(word);
if (this.changeableWordSize)
{
result.AddRange(OneStepExtraCharacter(word));
result.AddRange(OneStepLessCharacter(word));
}
_oneStepMemoization.TryAdd(word, result);
return result;
}
/// <summary>Returns words that are 1 char away from the word given.</summary>
private List<string> Onestep(string word)
{
List<string> result;
result = new List<string>();
char[] wordArr = word.ToCharArray();
char[] possibleWordArr = new char[wordArr.Length];
for (int i = 0; i < wordArr.Length; i++)
{
wordArr.CopyTo(possibleWordArr, 0);
foreach (char c in az)
{
possibleWordArr[i] = c;
string possibleWord = new string(possibleWordArr);
if (WordList.Contains(possibleWord))
{
result.Add(possibleWord);
}
}
}
return result;
}
/// <summary>Returns all valid words where 1 character has been removed.</summary>
private IEnumerable<string> OneStepLessCharacter(string word)
{
List<string> result = new List<string>();
char[] wordArr = word.ToCharArray();
char[] possibleWordArr = new char[wordArr.Length - 1];
for (int i = 0; i < wordArr.Length; i++)
{
for (int j = 0; j < possibleWordArr.Length; j++)
{
if (j != i)
{
possibleWordArr[j] = wordArr[j];
}
}
string possibleWord = new string(possibleWordArr);
if (WordList.Contains(possibleWord))
{
result.Add(possibleWord);
}
}
return result;
}
/// <summary>Returns all valid words where 1 character has been added.</summary>
private IEnumerable<string> OneStepExtraCharacter(string word)
{
List<string> result = new List<string>();
char[] wordArr = word.ToCharArray();
char[] possibleWordArr = new char[wordArr.Length + 1];
for (int i = 0; i < wordArr.Length; i++)
{
int offset = 0;
for (int j = 0; j < possibleWordArr.Length; j++)
{
if (j != i)
{
possibleWordArr[j] = wordArr[j - offset];
}
else
{
possibleWordArr[j] = '_'; // this is the extra character position
offset = 1;
}
}
foreach (char c in az)
{
possibleWordArr[i] = c;
string possibleWord = new string(possibleWordArr);
if (WordList.Contains(possibleWord))
{
result.Add(possibleWord);
}
}
}
return result;
}
private void LoadDictionary(string location)
{
WordList = new HashSet<string>();
foreach (string word in System.IO.File.ReadAllLines(location))
{
WordList.Add(word);
}
}
private void LoadCharAZ()
{
az = new List<char>();
for (int i = 'a'; i <= 'z'; i++)
{
az.Add((char)i);
}
}
}
}