using System.Collections.Generic;
using System.Threading.Tasks;
public static void Main()
string text = "ushpsghtgapajklhjkhghtjklyghtusus";
var pattern = GetMatchedPattern(text, 4);
foreach(var key in pattern.Keys){
Console.WriteLine(" Pattern " + key + " repeated " + pattern[key] + " no of times");
Console.WriteLine("Not able to find any patterns");
static Dictionary<string, int> GetMatchedPattern(string strtext, int len)
Dictionary<string, int> patterns = new Dictionary<string, int>();
for (int ichIndex = 0; ichIndex < strtext.Length; ichIndex++)
if (ichIndex + len <= strtext.Length)
if (!patterns.ContainsKey(strtext.Substring(ichIndex, len)))
patterns.Add(strtext.Substring(ichIndex, len), 1);
patterns[strtext.Substring(ichIndex, len)] = patterns[strtext.Substring(ichIndex, len)] + 1;
return patterns.Where(k => k.Value > 1).ToDictionary(y => y.Key, x => x.Value);