LeetCode-49.Group Anagrams

https://leetcode.com/problems/anagrams/

Given an array of strings, group anagrams together.

For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"]
Return:

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

Note:

  1. For the return value, each inner list's elements must follow the lexicographic order.
  2. All inputs will be in lower-case.

 最直接的做法,使用242题 LeetCode-242.Valid Anagram 的答案,可惜数据量大时超时

public class Solution
{
    public IList<IList<string>> GroupAnagrams(string[] strs) 
    {
        IList<IList<string>> res = new List<IList<string>>();
        int n = strs.Length,j=0;
        if (n == 0)
            return res;
        Array.Sort(strs);
        IList<string> list = new List<string>();
        list.Add(strs[0]);
        res.Add(list);
        if (n == 1)
            return res;
        
        for (int i = 1; i < n; i++)
        {
            for(j=0;j<res.Count;j++)
            {
                if (IsAnagram(res[j][0], strs[i]))
                {
                    res[j].Add(strs[i]);
                    break;
                }   
            }
            if (j == res.Count)
            {
                list = new List<string>();
                list.Add(strs[i]);
                res.Add(list);
            }
        }
        return res;
    }
    
    public bool IsAnagram(string s, string t)
    {
        int n = s.Length;
        if (t.Length != n)
            return false;
        int[] table = new int[26];
        for (int i = 0; i < n; i++)
        {
            table[s[i] - 'a']++;
            table[t[i] - 'a']--;
        }
        for (int i = 0; i < 26; i++)
        {
            if (table[i] != 0)
                return false;
        }
        return true;
    }
}

使用哈希表
public IList<IList<string>> GroupAnagrams(string[] strs) 
    {
        int n = strs.Length;
        if (n == 0)
            return new List<IList<string>>();
        Array.Sort(strs);
        Hashtable table = new Hashtable();
        for (int i = 0; i < n; i++)
        {
            char[] c = strs[i].ToCharArray();
            Array.Sort(c);
            string temp = new string(c);
            if (!table.Contains(temp))
                table[temp] = new List<string>();
            ((IList<string>)table[temp]).Add(strs[i]);
        }
        
        IList<IList<string>> res = new List<IList<string>>();
        foreach (DictionaryEntry item in table)
            res.Add((IList<string>)item.Value);
        return res;
    }

比较作弊的办法,使用C#的Linq
public class Solution
{
    public IList<IList<string>> GroupAnagrams(string[] strs) 
    {
        int n = strs.Length;
        if (n == 0)
            return new List<IList<string>>();
        Array.Sort(strs);
        var lookup = strs.ToLookup(word => SortLetters(word));
        IList<IList<string>> res = new List<IList<string>>();
        foreach (var item in lookup)
           res.Add(new List<string>(item));
        return res;
    }

    private string SortLetters(string word)
    {
        char[] letters = word.ToCharArray();
        Array.Sort(letters);
        return new string(letters);
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值