Group Anagrams

Given an array of strings, group anagrams together.


Input: ["eat", "tea", "tan", "ate", "nat", "bat"],


  • All inputs will be in lowercase.
  • The order of your output does not matter.

Leetcode 6th day challenge

C# Program

Solution – Easy to Understand

public class Solution {
    public IList<IList<string>> GroupAnagrams(string[] strs) {
        var map = new Dictionary<string,List<string>>();
        foreach(string s in strs)
            int[] hash = new int[26];
            for(int i=0;i<s.Length;i++)
            StringBuilder sb = new StringBuilder(s.Length);
            for(int i=0;i<26;i++)
                if(hash[i] > 0)
                    sb.Append((char)(i +'a'), hash[i]);
            string key = sb.ToString();
                map.Add(key,new List<string>());
        return map.Values.ToArray();

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s