using System;
using System.Collections.Generic;
using System.Text;
namespace MyCompiler {
class Program {
public static IList<IList<string>> GroupAnagrams(string[] strs) {
var result = new List<IList<string>>();
var seen = new Dictionary<string, List<string>>();
// Go through the anagrams
foreach (var anagram in strs) {
// Create a hash for this anagram
var hash = Hash(anagram);
// Check if this hash key exists
Console.WriteLine("Printing Dictionary values at this string {0}",anagram);
Console.WriteLine(String.Join(",",hash));
if (!seen.ContainsKey(hash)) {
seen[hash] = new List<string>();
}
// Add anagram to matching bucket
seen[hash].Add(anagram);
}
Console.WriteLine("Value in Final Dictionary");
Console.WriteLine(String.Join(",",seen));
// Build result
foreach (var pair in seen) {
result.Add(pair.Value);
}
return result;
}
private static string Hash(string input) {
var alphabet = new int[26];
foreach (var ch in input) {
++alphabet[ch - 'a'];
}
var sb = new StringBuilder();
for (int position = 0; position < alphabet.Length; ++position) {
if (alphabet[position] > 0) {
sb.Append(alphabet[position]);
sb.Append((char)('a' + position));
}
}
return sb.ToString();
}
public static void Main(string[] args) {
string[] str = new string[] {"eat","tea","tan","ate","nat","bat"};
var list = GroupAnagrams(str);
foreach(var item in list){
Console.WriteLine(String.Join(",",item));
}
Console.WriteLine("Hello world!");
}
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: