LeetCode-49.Group Anagrams

来源:互联网 发布:c json 双引号转义 编辑:程序博客网 时间:2024/05/01 23:56

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
原创粉丝点击