# -*- 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. Delphi之通过代码示例学习XML解析、StringReplace的用法(异常控制 good)

    *Delphi之通过代码示例学习XML解析.StringReplace的用法 这个程序可以用于解析任何合法的XML字符串. 首先是看一下程序的运行效果: 以解析这样一个XML的字符串为例: <? ...

  2. 大规模集群FTP代理(基于lvs的vsftpd网络负载均衡方案部署(PASV))

    [目的] 在日常工作中,我们经常需要在某服务器上开FTP(Server)服务.但就是这么简单的事情通常也会变得很复杂,原因如下:1.需要开通FTP的服务器没有公网IP地址:(即不能直接访问到)2.这样 ...

  3. Validate Binary Search Tree 解答

    Question Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is d ...

  4. 剑指offer-面试题22.栈的压入,弹出序列

    题目:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第 二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等. 例如序列1.2.3.4.5是某栈的压栈序列,序列4.5.3.2.1 是该压栈 ...

  5. GridBagLayout练习

    摘自http://blog.csdn.net/qq_18989901/article/details/52403737  GridBagLayout的用法 GridBagLayout是面板设计中最复杂 ...

  6. Unity 集成联通SDK

    我相信Unity程序员都会遇到加入SDK的问题,我相信如果你不会android编程,我相信你的CPU当场计算过快而爆炸!  这里也写笔记希望能帮助大家 如果有讲错的地方,希望大家能回复并且提供答案!  ...

  7. 格而知之4:寻找EXC_BAD_ACCESS

    EXC_BAD_ACCESS算是一个比较常见的错误,大部分情况下,它出现在某个对象还未初始化或已被释放后,还去试图访问这个对象的时候,即是在出现悬挂指针的时候(当然也有非悬挂指针导致的EXC_BAD_ ...

  8. java模拟get/post提交

    1:用jdk连接 String action = "xxxxxxxxxxx": URL url = new URL(action); HttpURLConnection http ...

  9. Android 代码混淆及第三方jar包不被混淆

    为了保护代码被反编译,android引入了混淆代码的概念 1.设置混淆 在工程下找到project.properties文件 在文件中加入proguard.config=${sdk.dir}/tool ...

  10. 【考虑周全+数学变形】【11月赛】Is it a fantastic matrix?

    Is it a fantastic matrix? Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/ ...