Created
January 30, 2013 23:58
-
-
Save drch-/4678548 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var words = File.ReadAllLines("words.txt").Select(x => new Word(x)).ToArray(); | |
var candidate = new Word("ethylenediaminetetraacetates"); | |
var watch = new Stopwatch(); | |
watch.Start(); | |
var matchedWords = words.Where(word => candidate.Contains(word)).ToList(); | |
watch.Stop(); | |
int wordCount = matchedWords.Count; | |
Console.WriteLine(wordCount + " words found in " + watch.ElapsedMilliseconds.ToString("00.00") + "ms"); | |
File.WriteAllLines(@"c:\temp\ethylenediaminetetraacetates.txt", matchedWords.Select(x => x.Original)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Word | |
{ | |
public string Original { get; set; } | |
private int[] _alphaIndex = new int[26]; | |
private int _letterFlag = 0; | |
public Word(string s) | |
{ | |
Original = s; | |
foreach (var ch in s) | |
{ | |
var ndx = ch - 'a'; | |
_alphaIndex[ndx]++; | |
_letterFlag |= 1 << ndx; | |
} | |
} | |
public bool Contains(Word other) | |
{ | |
if ((_letterFlag & other._letterFlag) != other._letterFlag) | |
{ | |
return false; | |
} | |
for (int i = 0; i < _alphaIndex.Length; i++) | |
{ | |
if (_alphaIndex[i] < other._alphaIndex[i]) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment