Best Way to Check if a Word is Valid from a .txt [closed]
Solution 1:
Well, 55k
English words are not that great volume. Assuming each word being 10
unicode characters (which seems to be an overestimation) we can expect
the required space to be 2 * 55000 * 10
~ 1 MB
. To validate words efficiently (scanning 55k
lines can be time consuming), try using HashSet<string>
:
using System.IO;
using System.Linq;
...
HashSet<string> allValidWords = new HashSet<string>(File
.ReadLines(@"c:\myWords.txt")
.Where(line => !string.IsNullOrWhiteSpace(line))
.Select(line => line.Trim()), StringComparer.OrdinalIgnoreCase);
Then use Contains
for validating:
string myWord = "Jabberwocky";
...
if (allValidWords.Contains(myWord)) {
// myWord is valid
}
else {
// myWord is not valid
}