14using System.Collections.Generic;
23 public int id {
get; }
24 public String word {
get; }
26 public WordInfo(
int id, String word)
33 private static readonly
int ARRAY_DIMENSION =
'z' -
'a';
34 private static readonly
int ARRAY_SIZE = (ARRAY_DIMENSION + 1) * (ARRAY_DIMENSION + 1);
36 private List<WordInfo>[] wordArray;
37 private int minWordSize = Int32.MaxValue;
41 wordArray =
new List<WordInfo>[ARRAY_SIZE];
44 public void addWord(
int id, String word)
47 if (word.Length < minWordSize)
49 minWordSize = word.Length;
52 String s = word.ToLower();
53 int index = (s[0] -
'a') * ARRAY_DIMENSION + s[1] -
'a';
54 if (index >= 0 && index < ARRAY_SIZE)
56 List<WordInfo> wList = wordArray[index];
59 wList =
new List<WordInfo>();
60 wordArray[index] = wList;
62 wList.Add(
new WordInfo(
id, s));
66 public HashSet<int> findWords(String text)
69 HashSet<int> ret =
new HashSet<int>();
71 String s = text.ToLower();
72 int dimension =
'z' -
'a';
73 for (
int i = 0; i < s.Length - (minWordSize - 1); i++)
77 if (c1 >=
'a' && c1 <= 'z' && c2 >=
'a' && c2 <=
'z')
79 int index = (c1 -
'a') * dimension + c2 -
'a';
80 List<WordInfo> l = wordArray[index];
83 foreach (WordInfo wi
in l)
85 if (s.Substring(i).StartsWith(wi.word))