原题地址:https://oj.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.

解题思路:anagram的意思是:abc,bac,acb就是anagram。即同一段字符串的字母的不同排序。将这些都找出来。这里使用了哈希表,即Python中的dict。针对前面的例子来讲,映射为{abc:abc,bac,acb}。

代码:

class Solution:
# @param strs, a list of strings
# @return a list of strings
def anagrams(self, strs):
dict = {}
for word in strs:
sortedword = ''.join(sorted(word))
dict[sortedword] = [word] if sortedword not in dict else dict[sortedword] + [word]
res = []
for item in dict:
if len(dict[item]) >= 2:
res += dict[item]
return res

[leetcode]Anagrams @ Python的更多相关文章

  1. LeetCode专题-Python实现之第28题: Implement strStr()

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  2. LeetCode专题-Python实现之第27题:Remove Element

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  3. LeetCode专题-Python实现之第26题:Remove Duplicates from Sorted Array

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  4. LeetCode专题-Python实现之第21题:Merge Two Sorted Lists

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  5. LeetCode专题-Python实现之第20题:Valid Parentheses

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  6. LeetCode专题-Python实现之第9题:Palindrome Number

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  7. LeetCode专题-Python实现之第14题:Longest Common Prefix

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  8. LeetCode专题-Python实现之第13题:Roman to Integer

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  9. LeetCode专题-Python实现之第7题:Reverse Integer

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

随机推荐

  1. centos yum 安装php mysql

    1 安装php7 查看 centos 版本 # cat /etc/centos-release 删除之前的 php 版本 # yum remove php* php-common rpm 安装 Php ...

  2. JFreeChart 之折线图

    JFreeChart 之折线图 一.JFreeChart 简介 JFreeChart是JAVA平台上的一个开放的图表绘制类库.它完全使用JAVA语言编写,是为applications, applets ...

  3. BZOJ.3143.[HNOI2013]游走(概率 期望 高斯消元)

    题目链接 参考 远航之曲 把走每条边的概率乘上分配的标号就是它的期望,所以我们肯定是把大的编号分配给走的概率最低的边. 我们只要计算出经过所有点的概率,就可以得出经过一条边(\(u->v\))的 ...

  4. 洛谷P4141消失之物

    题目描述 ftiasch 有 N 个物品, 体积分别是 W1, W2, …, WN. 由于她的疏忽, 第 i 个物品丢失了. “要使用剩下的 N – 1 物品装满容积为 x 的背包,有几种方法呢?” ...

  5. 在.net core 2.0中生成exe文件

    .net core 2.0程序默认生成的是一个dll,需要通过dotnet命令来执行他. dotnet ConsoleApp1.dll 这种方式有点类似于java程序.本身这种方式没有什么问题,但在调 ...

  6. TC SRM600 DIV2

    开始补TC. 目前才补完SRM600的DIV2. 题目:http://community.topcoder.com/tc?module=ProblemArchive 250: 很水,不解释,直接搞. ...

  7. 分享下使用 svn,测试服务器代码自动更新、线上服务器代码手动更新的配置经验

    分享下使用 svn,测试服务器代码自动更新.线上服务器代码手动更新的配置经验 利用SVN的POST-COMMIT钩子自动部署代码 Linux SVN 命令详解 Linux SVN 命令详解2 使用sv ...

  8. 《Go学习笔记 . 雨痕》流程控制(if、switch、for range、goto、continue、break)

    Go 精简(合并)了流控制语句,虽然某些时候不够便捷,但够用. if...else... 条件表达式值必须是布尔类型,可省略括号,且左花括号不能另起一行. func main() { x := 3 i ...

  9. Java 微服务实践 - Spring Boot 系列

    https://segmentfault.com/l/1500000009515571

  10. (Delphi) Using the Disk Cache 使用磁盘缓存

    The Chilkat Spider component has disk caching capabilities. To setup a disk cache, create a new dire ...