[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", " ...
随机推荐
- JavaScript为unicode编码转换为中文
代码laycode - v1.1 关于这样的数据转换为中文问题,常用的以下方法. 1. eval解析或new Function("'+ str +'")() str = eval ...
- 【转】Android虚拟平台的编译和整合
原文网址:http://blog.csdn.net/rickleaf/article/details/6369065 概要 Android从2008年开始到本文写的2011年,短短三年的时间里成为手机 ...
- initial pointer [expert c]
initial differece between pointer and array Both arrays and pointers can be initialized with a liter ...
- The Java™ Tutorials下载地址
1.The Java™ Tutorials下载地址: http://www.oracle.com/technetwork/java/javase/java-tutorial-downloads-200 ...
- PHP 9 大缓存技术总结
1.全页面静态化缓存 也就是将页面全部生成html静态页面,用户访问时直接访问的静态页面,而不会去走php服务器解析的流程.此种方式,在CMS系统中比较常见,比如dedecms: 一种比较常用的实现方 ...
- Red and Black(简单dfs)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 怎样查询SCI和EI检索号
为了年终考核,花了一个早上才搞清楚,里面有非常多小问题.以下具体说明具体过程: SCI检索号 1.进入图书馆主页: 2.选择"电子数据库": 3.选择外文数据库中的"We ...
- hdu 1078 FatMouse and Cheese 记忆化dp
只能横向或竖向走...一次横着竖着最多k步...不能转弯的.... 为毛我的500+ms才跑出来... #include<cstdio> #include<iostream> ...
- 《你必须知道的495个C语言问题》知识笔记及补充
1. extern在函数声明中是什么意思? 它能够用作一种格式上的提示表明函数的定义可能在还有一个源文件里.但在 extern int f(); 和 int f(); 之间并没有实质的差别. 补充:e ...
- Unity Layout碰撞检测
第一次看到LayerMask根本不知道是什么东东,后来问问度娘,看了几篇文章,终于看明白一点点,在网上看到各路大神的解释,终于明白了,LayerMask实际上是一个位码操作,在Unity3d中Laye ...