[Leetcode][Python]49: Anagrams
# -*- 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的更多相关文章
- 【LeetCode】49. Anagrams (2 solutions)
Anagrams Given an array of strings, return all groups of strings that are anagrams. Note: All inputs ...
- 【LeetCode】49. Group Anagrams 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+hash 日期 题目地址:https://le ...
- 49. Anagrams
题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...
- Leetcode Python Solution(continue update)
leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...
- LeetCode python实现题解(持续更新)
目录 LeetCode Python实现算法简介 0001 两数之和 0002 两数相加 0003 无重复字符的最长子串 0004 寻找两个有序数组的中位数 0005 最长回文子串 0006 Z字型变 ...
- Python 解leetcode:49. Group Anagrams
题目描述:给出一个由字符串组成的数组,把数组中字符串的组成字母相同的部分放在一个数组中,并把组合后的数组输出: 思路: 使用一个字典,键为数组中字符串排序后的部分,值为排序后相同的字符串组成的列表: ...
- 【一天一道LeetCode】#49. Group Anagrams
一天一道LeetCode系列 (一)题目 Given an array of strings, group anagrams together. For example, given: [" ...
- Leetcode#49 Anagrams
原题地址 Anagram:变位词.两个单词是变位词关系的条件是:组成单词的字符相同,只是顺序不同 第一次看这道题看了半天没明白要干嘛,丫就不能给个样例输入输出么..后来还是看网上其他人的总结知道是怎么 ...
- 【LeetCode】49. Group Anagrams
题目: Given an array of strings, group anagrams together. For example, given: ["eat", " ...
随机推荐
- mybatis第一个入门demo
学习框架技术,一般先写个demo,先知道是什么,然后在知道为什么,这也是进步的一种. 源码链接:http://pan.baidu.com/s/1eQJ2wLG
- 常用数据结构及复杂度 array、LinkedList、List、Stack、Queue、Dictionary、SortedDictionary、HashSet、SortedSet
原文地址:http://www.cnblogs.com/gaochundong/p/data_structures_and_asymptotic_analysis.html 常用数据结构的时间复杂度 ...
- Windows多线程同步系列之四-----信号量
信号量说实话自己没怎么使用过.书上大概这样说,信号量设置一个资源访问计数.当该计数值大于0的时候,该信号量对象 为有信号状态,当该计数值等于0的时候,该信号量对象为无信号状态. 我们来查几个主要的AP ...
- LeeCode-Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has For exampl ...
- HDOJ-1018 Big Number
http://acm.hdu.edu.cn/showproblem.php?pid=1018 题意:给出一个数n,输出n的阶乘的位数 汗Σ( ° △ °|||)︴刚开始还准备上大数乘法 然而10000 ...
- Impala 3、Impala、Hbase整合
Impala可以通过Hive外部表方式和HBase进行整合,步骤如下: • 步骤1:创建hbase 表,向表中添加数据 create 'test_info', 'info' put 'test_inf ...
- iOS openURL方法实现打电话、发短信、发邮件、打开其他App
UIApplication有个功能十分强大的openURL:方法 - (BOOL)openURL:(NSURL*)url; 通过这个方法,我们可以实现: 先获取 UIApplication UIApp ...
- 网站SEO优化中内部链接的优化
重要性:内链有效的优化能够间接的提高某页面的权重达到搜索排名靠前的效果.同时有效的带领搜索引擎蜘蛛对整站进行抓取. 网站头部导航: 这个导航称为'网站主导航',当用户来到网站需要给他们看到的内容.也就 ...
- HBase的基本操作
1.输入hbase shell进入HBase shell
- telnet如何操作Memcached缓存系统?
4.(1)telnet操作Memcached 许多语言都实现了连接memcached的客户端,其中以Perl.PHP为主.仅仅memcached网站上列出的语言就有:• Perl • PHP • Py ...