using System.Collections.Generic;
public static string Srch { get; set; }
public static void Main()
Srch = "zfabcde224lkfabc51+crsdtab=";
StringUtility stringUtility = new StringUtility(Srch, 3);
if (stringUtility.Result != null)
(stringUtility.Result.ContainsKey("abc") && stringUtility.Result["abc"] == 2)
(stringUtility.Result.ContainsKey("fab") && stringUtility.Result["fab"] == 2)
Console.WriteLine("Current Srch String : '" + Srch + "' has '" + stringUtility.Result.Count + "' distinct patterns.", ConsoleColor.Green);
var duplicatesDict = stringUtility.Result.Where(p => p.Value > 1).ToDictionary(t => t.Key, t => t.Value);
if (duplicatesDict != null && duplicatesDict.Count > 0)
Console.WriteLine("There are / is duplicate occurence(s) : '" + duplicatesDict.Count + "' of them.", ConsoleColor.Green);
foreach (var item in duplicatesDict)
Console.WriteLine("Current item : '" + item.Key + "' has '" + item.Value + "' occurences.", ConsoleColor.Green);
Console.WriteLine("Press any key to quit");
public static void SearchPatternLengthValidation()
StringUtility stringUtility = new StringUtility(srch, patternLength);
if (stringUtility.Result == null)
Console.WriteLine("test #1 we check to see validations - Result IS Null - Passed", ConsoleColor.Green);
Console.WriteLine("test #1 we check to see validations - Result IS Not Null - Falied", ConsoleColor.Red);
public static void SearchPatternLengthBuild()
string srch = "abcdefabc";
StringUtility stringUtility = new StringUtility(srch, patternLength);
if (stringUtility.Result != null && stringUtility.Result.Count == 6)
Console.WriteLine("test #2 we need to carve up the string into distinct chunks - Result Is Not Null - Passed", ConsoleColor.Green);
Console.WriteLine("test #2 we carve up - Falied", ConsoleColor.Red);
public static void SearchPatternLengthCount()
string srch = "abcdefabc";
StringUtility stringUtility = new StringUtility(srch, patternLength);
if (stringUtility.Result != null && stringUtility.Result.Count == 6)
if (stringUtility.Result.ContainsKey("abc") && stringUtility.Result["abc"] == 2)
Console.WriteLine("test #3 we need to count distinct chunks - Result Is Not Null and We have 2 abc's in the list - Passed", ConsoleColor.Green);
Console.WriteLine("test #3 we need to count distinct chunks - - Falied", ConsoleColor.Red);
public static void SearchPatternLengthCountComplex()
StringUtility stringUtility = new StringUtility(srch, patternLength);
if (stringUtility.Result != null && stringUtility.Result.Count == 23)
(stringUtility.Result.ContainsKey("abc") && stringUtility.Result["abc"] == 2)
(stringUtility.Result.ContainsKey("fab") && stringUtility.Result["fab"] == 2)
Console.WriteLine("test #3 we need to count distinct chunks - Result Is Not Null and We have 2 abc's / fab's in the list - Passed", ConsoleColor.Green);
Console.WriteLine("test #3 we need to count distinct chunks - - Falied", ConsoleColor.Red);
public static void SearchPatternLengthCountComplexPattern2()
StringUtility stringUtility = new StringUtility(srch, patternLength);
if (stringUtility.Result != null && stringUtility.Result.Count > 0)
(stringUtility.Result.ContainsKey("ab") && stringUtility.Result["ab"] == 3)
Console.WriteLine("test #3 we need to count distinct chunks - Result Is Not Null and We have 3 ab's in the list when pattern is 2 - Passed", ConsoleColor.Green);
Console.WriteLine("test #3 we need to count distinct chunks - - Falied", ConsoleColor.Red);
public class StringUtility
public StringUtility(string srch, int patternLength)
this.patternLength = patternLength;
private Dictionary<string, int> result;
public Dictionary<string, int> Result
private int patternLength;
if (!string.IsNullOrEmpty(srch) && patternLength > 0)
this.result = new Dictionary<string, int>();
for (int index = 0; index <= srch.Length - patternLength; index++)
string item = srch.Substring(index, patternLength);
if (!string.IsNullOrEmpty(item) && item.Length == patternLength)
if (!this.result.ContainsKey(item))
this.result.Add(item, 0);
if (this.result != null && this.result.Count > 0)
List<string> keys = this.result.Keys.Where(p => string.IsNullOrEmpty(p) == false).ToList();
foreach (var pattern in keys)
int index = srch.IndexOf(pattern, startIndex);
startIndex = index + pattern.Length;