using System.Collections.Generic;
public class FrozenMountain
static public IEnumerable<string> ConvertStringToArraySize(string sInputString, int nPatternLength)
for (int index = 0; index <= sInputString.Length - nPatternLength; index++)
yield return sInputString.Substring(index, nPatternLength);
static public Dictionary<string,int> Pattern_Finder_Count(string sInputString, int nPatternLength, string sCase)
throw new ArgumentException("Pattern length should be greater than '0'","nPatternLength");
if(sInputString == null || sInputString.Length == 0)
throw new ArgumentException("Input string can not be null or empty.","sInputString");
if (nPatternLength > sInputString.Length)
throw new ArgumentException("Pattern length can not be greater than input string length", "nPatternLength");
var result = ConvertStringToArraySize(sInputString, nPatternLength);
StringComparer ignoreCase = StringComparer.InvariantCulture;
if( sCase.Equals("y") || sCase.Equals("Y"))
ignoreCase = StringComparer.InvariantCultureIgnoreCase;
return result.GroupBy(x => x, ignoreCase).Where(gr => gr.Count()>1).ToDictionary(gr => gr.Key, gr => gr.Count());
public static void Main()
Console.WriteLine("Enter an input string");
string sInputString = Console.ReadLine();
Console.WriteLine("Ignore case sensitive [y/n]");
string sCase = Console.ReadLine();
Console.WriteLine("Enter a pattern length");
if(int.TryParse(Console.ReadLine(), out nPatternLength))
Dictionary<string, int> patterns = new Dictionary<string, int>();
patterns = Pattern_Finder_Count(sInputString, nPatternLength, sCase);
foreach(KeyValuePair<string, int> pattern in patterns)
Console.WriteLine("Pattern : " + pattern.Key.ToString() + " - Occurence: " + pattern.Value.ToString());
else Console.WriteLine("There are no patterns of length "+ nPatternLength);
else Console.WriteLine("Input length is not a valid number");