using System.Collections.Generic;
public static void Main()
var patterns = FindAllPatterns("zfabcde224lkfabc51+crsdtab=", 3);
PrintAllPatterns(patterns);
static List<Pattern> FindAllPatterns(string input, int patternLength)
InputValidation(input, patternLength);
List<Pattern> listOfPatterns = new List<Pattern>();
Dictionary<string, int> temp = new Dictionary<string, int>();
for (int i = 0; i < input.Length; i++)
bool cannotFormPattern = (i + patternLength - 1) > input.Length - 1;
if (cannotFormPattern){ break; }
string pattern = input.Substring(i, patternLength);
temp[pattern] = temp.ContainsKey(pattern) ? temp[pattern] + 1 : 1;
foreach (var item in temp)
listOfPatterns.Add(new Pattern(item.Key, item.Value));
foreach (var item in temp)
listOfPatterns.Add(new Pattern(item.Key, item.Value));
static void InputValidation(string input, int patternLength)
if (patternLength > input.Length - 1 || patternLength == 0 || input == string.Empty)
throw new NullReferenceException("invalid values entered...");
static void PrintAllPatterns(List<Pattern> patterns)
foreach (var pattern in patterns)
Console.WriteLine("pattern: " + pattern.Value + "\nnumber of occurance: " + pattern.Occurances);
Console.WriteLine("There are no patterns that occured more than once...");
public string Value { get; set; }
public int Occurances { get; set; }
public Pattern(string value, int occurances)
this.Occurances = occurances;