作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/find-common-characters/

题目描述

Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer.

You may return the answer in any order.

Example 1:

Input: ["bella","label","roller"]
Output: ["e","l","l"]

Example 2:

Input: ["cool","lock","cook"]
Output: ["c","o"]

Note:

  1. 1 <= A.length <= 100
  2. 1 <= A[i].length <= 100
  3. A[i][j] is a lowercase letter

题目大意

给出了一个字符串列表,每个字符串都只包含小写字符,如果某个字符在所有的字符串中均出现,那么就把这个字符放到结果列表中。注意,如果同样的字符出现了不止一次,那么需要放等量的数目到结果中。

解题方法

字典

毕竟周赛第一题,比较简单。只要明白题意之后,我们就知道了这个题可以用字典解决。

使用字典统计每个字符串中的每个字符的次数,然后对26个字符进行遍历,统计在所有字符串中这个字符出现的最少次数,把该最小次数个该字符放到结果列表中即可。

问:在统计完一个小写字符之后需要把每个字符串对应的字典中减去该最小次数吗?

答:不需要,因为我们接着就会遍历下一个字符,当前字符出现的次数不影响。

python代码如下:

class Solution(object):
def commonChars(self, A):
"""
:type A: List[str]
:rtype: List[str]
"""
A_count = map(lambda x : collections.Counter(x), A)
res = []
for i in range(26):
c = chr(ord('a') + i)
min_count = min([a_count[c] for a_count in A_count])
if min_count:
res.extend([c] * min_count)
return res

日期

2019 年 3 月 3 日 —— 3月开始,春天到了

最新文章

  1. AngularJS下拉列表select在option动态变化之后多出了一个错误项的问题
  2. 20161020001 DataGridView 选中的 DataGridViewCheckBoxCell 不添加重复项
  3. hibernate 入门([数据访问中间件] 开源框架)
  4. yii2增加验证码详细步骤
  5. BigDecimal 转换类型
  6. Python语言初学总结
  7. ExtJs尝下鲜
  8. C语言中 v...printf类函数的用法
  9. 计算字符串和文件的MD5值
  10. Linux网络编程(五)
  11. 【译】TensorFlow Python Docs 之 overview
  12. 搭建本地git仓库
  13. Maven中settings.xml的配置项说明精讲
  14. 如何从Eclipse导入github上的项目源码--转载
  15. oracle入坑日记&lt;三&gt;用户详解(角色理解)
  16. 第二个Sprint
  17. jQuery方法实现
  18. 技术向:一文读懂卷积神经网络CNN
  19. &quot; java.lang.NoSuchFieldError: HBASE_CLIENT_PREFETCH_LIMIT
  20. hdu-6301-贪心

热门文章

  1. R语言因子排序
  2. 在WEB网页上模拟人的操作(批量操作)
  3. Python压缩&amp;解压缩
  4. [转载]ORA-02287: 此处不允许序号
  5. Hadoop入门 完全分布式运行模式-集群配置
  6. Angular中@Output()的使用方法
  7. lvm 创建扩展
  8. OC中的结构体
  9. Dubbo提供者的异步执行
  10. 【Linux】【Services】【Disks】bftfs