Given an array of strings, group anagrams together.

Example:

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

Note:

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

思路


  这道题在一开始看到的时候,没有什么具体的思路。但是在想了一会之后觉得可以使用辅助字段来解决该问题。意思就是我们对列表中的字符串进行排序,然后将相同的添加进同一列表中。当遍历完毕之后得到最后的结果。时间复杂度为O(n mlog m),其中n为列表的长度,m为其中最长的字符串长度。空间复杂度为O(n*m)。

  在后面看到别人的解答之后学习到了另一种思路就是因为字符只有26个,所有我们设置一个列表,其中包含26个0,然后对列表中每一个字符串进行统计相应的字母出现的次数,然后把他加到相应的位置,然后将其转化为元祖并利用字典,将其对应的字符串添加到对应的列表中。时间复杂度复杂度为O(n*m), n为列表的长度,m为最长的单词数。空间复杂度为O(n*m)。

第一种思路的解决代码


  1. class Solution(object):
  2. def groupAnagrams(self, strs):
  3. ans = collections.defaultdict(list) # 辅助字典
  4. for s in strs:
  5. ans[tuple(sorted(s))].append(s) # 排序之后将其添加到对应的列表中
  6. return ans.values() # 返回列表

第二种思路的解决代码


  1. class Solution(object):
  2. def groupAnagrams(self, strs):
  3. hmap = collections.defaultdict(list)
  4. for st in strs:
  5. array = [0] * 26 # 26个元素的列表
  6. for l in st: # 将字符串元素计数
  7. array[ord(l) - ord('a')] += 1
  8. hmap[tuple(array)].append(st) # 按照元素出现的个数进行添加
  9. return hmap.values()

【LeetCode每天一题】Group Anagrams(变位词组)的更多相关文章

  1. [leetcode]49. Group Anagrams变位词归类

    Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...

  2. LeetCode(49)Group Anagrams

    题目 Given an array of strings, group anagrams together. For example, given: ["eat", "t ...

  3. [LeetCode] 49. Group Anagrams 分组变位词

    Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...

  4. LeetCode第[49]题(Java):Group Anagrams

    题目:同字符分组 难度:Medium 题目内容: Given an array of strings, group anagrams together. 翻译:给定一组字符串数组,按相同字符组成的字符 ...

  5. LeetCode 49: 字母异位词分组 Group Anagrams

    LeetCode 49: 字母异位词分组 Group Anagrams 题目: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. Given an array o ...

  6. leetcode@ [49] Group Anagrams (Hashtable)

    https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...

  7. 【一天一道LeetCode】#49. Group Anagrams

    一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: [" ...

  8. [LeetCode] Group Anagrams 群组错位词

    Given an array of strings, group anagrams together. Example: Input: ["eat", "tea" ...

  9. LeetCode OJ 49. Group Anagrams

    题目 Given an array of strings, group anagrams together. For example, given: ["eat", "t ...

随机推荐

  1. Python2安装igraph

    前言 igraph是一个进行图计算和社交网络分析的软件包,支持python语言,打算学习igraph,然后应用在自己的项目中. 系统环境 64位win10系统,同时安装了python3.6和pytho ...

  2. python操作文件

    OS模块 1.getcwd() 用来获取当前工作目录 >>> import os >>> os.getcwd() 'D:\\Postgraduate\\Python ...

  3. Ajax请求参数较长导致请求失败

    Ajax请求参数比较长,第5行参数大概1100个字符吧,是接口的请求报文. $.ajax({ type:"POST", url:"${ctx}/test.action?m ...

  4. MySQL的sql_mode模式说明及设置

    MySQL的sql_mode模式说明及设置 MySQL的sql_mode合理设置 sql_mode是个很容易被忽视的变量,默认值是空值,在这种设置下是可以允许一些非法操作的,比如允许一些非法数据的插入 ...

  5. [No0000141]Outlook,设置全局已读回执

    Outlook,设置全局已读回执 文件->选项

  6. arcengine直连sde

    搞了半天,找了好多资料,实验好多次,终于解决.参考资料:http://www.cnblogs.com/gaxin/p/5777864.html

  7. [daily][mirror][daemonlogger][tc] 我想把一个网卡(port A)的流量,镜像到虚拟机的一个网卡(port VA)上去

    iptables tee 模块 https://blog.gnuers.org/?p=740 http://blog.csdn.net/wesleyflagon/article/details/385 ...

  8. iOS将excel转plist

    iOS将excel转plist 先把excel用Numbers打开,转换成CSV,然后新建一个工程,写下面的代码: - (void)viewDidLoad { [super viewDidLoad]; ...

  9. JavaScript 学习笔记-HTML&&DOM

    HTML DOM (文档对象模型) 当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model). HTML DOM 模型被构造为对象的树. JavaScript 能够 ...

  10. vue-loader是什么?使用它的用途有哪些?

    vue-loader是解析 .vue 文件的一个加载器,跟 template/js/style转换成 js 模块: 用途:js可以写es6.style样式可以scss或less:template可以加 ...