Skip to content

Instantly share code, notes, and snippets.

@drch-
Created January 30, 2013 23:58
Show Gist options
  • Save drch-/4678548 to your computer and use it in GitHub Desktop.
Save drch-/4678548 to your computer and use it in GitHub Desktop.
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));
}
}
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