[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", " ...
随机推荐
- linux source命令学习
1. linux source命令的作用? 我们可能经常需要修改到诸如/etc/profile,~/.bash_profile等这样的配置文件, 一方面我们希望所作的修改在当前的环境中立即生效: 另一 ...
- C语言的本质(34)——静态库
库是一种软件组件技术,库里面封装了数据和函数. 库的使用可以使程序模块化. Windows系统包括静态链接库(.lib文件)和动态链接库(.dll文件). Linux通常把库文件存放在/usr/lib ...
- cf472A Design Tutorial: Learn from Math
A. Design Tutorial: Learn from Math time limit per test 1 second memory limit per test 256 megabytes ...
- Add Digits 解答
Question Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- hdu 1502 Regular Words_高精度+dp
题意:问按规则排成的串有多少个A(c)>= B(c) >= C(c) 思路:因为写大整数太累,就偷懒了一下直接用java水过 import java.math.BigInteger; im ...
- Debug of bash , perl and python
1.bash -x script 2.perl -d script 3. python -m pdb script n: execute the next line s: execute the ...
- iOS 10 (X8)上CoreData的使用(包含创建工程时未添加CoreData)
1.在创建工程时未添加CoreData,后期想要使用CoreData则要在工程Appdelegate.h文件中添加CoreData库和CoreData中的通道类(用来管理类实例和CoreData之间的 ...
- Linux环境Nginx安装多版本PHP
php5.4.44 所谓多版本多版本PHP就是php5.4和5.5以及其他版本在同一台服务器. 假如php5.5是主版本已经安装在/usr/local/php目录下,那么再安装其他版本的php再指定不 ...
- IOS 打印网络请求全链接
NSMutableString *urlStr = [NSMutableString stringWithFormat:@"%@?",request.URL]; ;i<[pa ...
- sqlite数据库读写在linux下的权限问题
近期在学linux,恰巧有个php项目要做.于是配置好环境打算在linux下做. 无奈站点执行后一片空白.经过调试发现是sqlite数据库的问题. 安装sqlite扩展 apt-get install ...