using System.Collections.Generic;
public static void Main()
string str = "In the end this is not the end";
var dict2 = groupCount(str, 2);
Console.WriteLine("Group Of 2:");
foreach ( string k in dict2.Keys ) {
Console.WriteLine("Key: \"{0}\", Count: {1}", k, dict2[k]);
var dict3 = groupCount(str, 3);
Console.WriteLine("Group Of 3:");
foreach ( string k in dict3.Keys ) {
Console.WriteLine("Key: \"{0}\", Count: {1}", k, dict3[k]);
public static Dictionary<string,int> groupCount(string str, int groupCount)
string[] tokens = str.Split(new char[] { ' ' });
var dict = new Dictionary<string,int>();
for ( int i = 0; i < tokens.Length - (groupCount-1); i++ )
for ( int j = 0; j < groupCount; j++ )
key += tokens[i+j] + " ";
key = key.Substring(0, key.Length-1);
if ( dict.ContainsKey(key) ) {