# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 49: Anagrams
https://leetcode.com/problems/anagrams/ Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case. === Comments by Dabay===
http://blog.csdn.net/linhuanmars/article/details/21664747
''' class Solution:
# @param strs, a list of strings
# @return a list of strings
def anagrams(self, strs):
hash_table = {}
for str in strs:
l = list(str)
l.sort()
sorted_str = ''.join(l)
if sorted_str in hash_table:
hash_table[sorted_str].append(str)
else:
hash_table[sorted_str] = [str]
res = []
for sorted_str in hash_table:
if len(hash_table[sorted_str]) > 1:
res.extend(hash_table[sorted_str])
return res def main():
sol = Solution()
strs = ["ab", "ba", "abc", "cba", "z"]
print sol.anagrams(strs) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]49: Anagrams的更多相关文章

  1. 【LeetCode】49. Anagrams (2 solutions)

    Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...

  2. 【LeetCode】49. Group Anagrams 解题报告(Python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+hash 日期 题目地址:https://le ...

  3. 49. Anagrams

    题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...

  4. Leetcode Python Solution(continue update)

    leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...

  5. LeetCode python实现题解(持续更新)

    目录 LeetCode Python实现算法简介 0001 两数之和 0002 两数相加 0003 无重复字符的最长子串 0004 寻找两个有序数组的中位数 0005 最长回文子串 0006 Z字型变 ...

  6. Python 解leetcode:49. Group Anagrams

    题目描述:给出一个由字符串组成的数组,把数组中字符串的组成字母相同的部分放在一个数组中,并把组合后的数组输出: 思路: 使用一个字典,键为数组中字符串排序后的部分,值为排序后相同的字符串组成的列表: ...

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

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

  8. Leetcode#49 Anagrams

    原题地址 Anagram:变位词.两个单词是变位词关系的条件是:组成单词的字符相同,只是顺序不同 第一次看这道题看了半天没明白要干嘛,丫就不能给个样例输入输出么..后来还是看网上其他人的总结知道是怎么 ...

  9. 【LeetCode】49. Group Anagrams

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

随机推荐

  1. HttpApplication实战大文件上传 (第四篇)

    一.Asp.net中的文件上传 在Asp.net 1.1中,文件在上传过程中将被全部保存在内存中,对于大文件来说,会造成内存空间的过度使用,可能会招致恶意攻击.为了解决这个问题,Asp.net在配置文 ...

  2. 场景2 nginx 错误日志格式:

    nginx 错误日志格式: 2016/09/01 11:23:36 [error] 28388#0: *14549 open() "/var/www/zjzc-web-frontEnd/im ...

  3. logstash 处理tomcat日志

    [root@dr-mysql01 tomcat]# cat logstash_tomcat.conf input { file { type => "zj_api" path ...

  4. 项目中Spring注入报错小结

    之前在做单元测试时采用注解方式进行service对象的注入,但运行测试用例时对象要注入的service对象总是空的,检查下spring配置文件,我要配置的bean类xml文件已经包含到spring要加 ...

  5. UESTC_秋实大哥掰手指 2015 UESTC Training for Dynamic Programming<Problem B>

    B - 秋实大哥掰手指 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 2048/1024KB (Java/Others) Submit ...

  6. 怎么用MindMapper分类功能整理导图

    我们在绘制导图时是按着我们大脑中的思维来的,虽然有着总体逻辑,但是在细节上面还是不可避免的有些小杂乱,我们则可以通过MindMapper分类功能来对导图进行整理. 我们在打开MindMapper思维导 ...

  7. 怎样为virtualbox添加新的分辨率

    virtualbox是个相当NB的开源跨平台虚拟机软件,只是新创建的虚拟机仅仅支持几种分辨率.比如.安装win8.1,仅仅有例如以下图的几种分辨率. 只是我的显示器是5K哦,这么点分辨率,简直是搞笑. ...

  8. Kolor Neutralhazer v1.0.2 (照片雾气模糊去除过滤器)+破解RI

    由于空气污染.阴霾几天越来越,根据照片始终是一个灰色,怎么做?有了这个插件.能够解除您的烦恼. Neutralhazer这是消除你的风景照片和雾气模糊的全景图的有效途径photoshop小工具. wa ...

  9. Jquery时间段选择器

    效果(有给小bug, 在时间的大小比较上.): HTML: <html> <head> <title>测试DatePicker</title> < ...

  10. java学习之Date的使用

    Date使用,主要要注意将日期格式化,否则返回的是系统默认的格式.请自己查阅API文档. import java.util.*; import java.text.*; public class Te ...