using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
class Program
{
static Dictionary<string, List<string>> GroupByFirstNLetters(string s, int n = 3) {
var groups = new Dictionary<string, List<string>>();
var regex = new Regex("[A-Za-z]+");
foreach (Match m in regex.Matches(s.ToLower())) {
string word = m.Value;
if (word.Length >= n) {
string prefix = word.Substring(0, n);
if (!groups.ContainsKey(prefix))
groups[prefix] = new List<string>();
groups[prefix].Add(word);
}
}
return groups;
}
static void Main()
{
string s = "The lowly inhabitants of the lowland were surprised to see " +
"the lower branches of the trees.";
var groups = GroupByFirstNLetters(s, 3);
// Print version 1
foreach (var kv in groups)
Console.WriteLine($"{kv.Key} : [{string.Join(", ", kv.Value)}]");
Console.WriteLine();
// Print version 2
foreach (var kv in groups)
Console.WriteLine($"{kv.Key}: {string.Join(", ", kv.Value)}");
}
}
/*
run:
the : [the, the, the, the]
low : [lowly, lowland, lower]
inh : [inhabitants]
wer : [were]
sur : [surprised]
see : [see]
bra : [branches]
tre : [trees]
the: the, the, the, the
low: lowly, lowland, lower
inh: inhabitants
wer: were
sur: surprised
see: see
bra: branches
tre: trees
*/