private Dictionary<char, Counters> FillDictionary(string pattern) { Dictionary<char, Counters> dic = new Dictionary<char, Counters>(); for (int i = 0; i < pattern.Length; i++) { if (!dic.ContainsKey(pattern[i])) { dic[pattern[i]] = new Counters(1, 1); } else { dic[pattern[i]] = new Counters(dic[pattern[i]].Original + 1, dic[pattern[i]].Original + 1); } } return dic; }
private bool IsMatches(string text, Dictionary<char, Counters> dic, int indexInText, int patternLength) { int charsNotMatched = patternLength; if (text.Length - indexInText < patternLength) { return false; } //zeroing counter.. foreach (var key in dic.Keys) { dic[key].Current = dic[key].Original; } for (int i = indexInText; i < indexInText + patternLength; i++) { char charecter = text[i]; if (dic.ContainsKey(charecter)) { dic[charecter] = new Counters(dic[charecter].Original, dic[charecter].Current - 1); if (dic[charecter].Current < 0) { return false; } charsNotMatched--; } } return charsNotMatched == 0; }