【LeetCode】1002. Find Common Characters 解题报告(Python)
作者: 负雪明烛
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 <= A.length <= 100
1 <= A[i].length <= 100
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月开始,春天到了
【LeetCode】1002. Find Common Characters 解题报告(Python)的更多相关文章
- LeetCode 1002. Find Common Characters (查找常用字符)
题目标签:Array, Hash Table 题目给了我们一个string array A,让我们找到common characters. 建立一个26 size 的int common array, ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- Leetcode 1002. Find Common Characters
python可重集合操作 class Solution(object): def commonChars(self, A): """ :type A: List[str] ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】647. Palindromic Substrings 解题报告(Python)
[LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...
- 【LeetCode】392. Is Subsequence 解题报告(Python)
[LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- 【LeetCode】809. Expressive Words 解题报告(Python)
[LeetCode]809. Expressive Words 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
随机推荐
- 除了GO基因本体论,还有PO、TO、CO等各种Ontology?
目录 PO/TO CO 后记 我们最常用最熟悉的功能数据库之一:GO(gene onotology),基因本体论.其实是一套标准词汇术语,目的是从不同角度来描述某个基因的特点和功能,三大本体如生物学进 ...
- 【机器学习与R语言】9- 支持向量机
目录 1.理解支持向量机(SVM) 1)SVM特点 2)用超平面分类 3)对非线性空间使用核函数 2. 支持向量机应用示例 1)收集数据 2)探索和准备数据 3)训练数据 4)评估模型 5)提高性能 ...
- vim ——批量注释
1111. ctrl+v进入列编辑模式,在编辑模式中,在行间上下移动光标,选择需要编辑哪些行的对应的列,之后使用I(大写)进入列插入.插入注释符,按两次Esc退出列编辑模式即可实现多行注释 删除注释: ...
- Linux—export命令查看、修改用户环境变量
Linux export 命令用于设置或显示环境变量. 在 shell 中执行程序时,shell 会提供一组环境变量. export 可新增,修改或删除环境变量,供后续执行的程序使用. export ...
- 学习Java的第三天
一.今日收获 1.今天家里有白事,忙了一整天,也没有看更多的资料 二.今日问题 无 三.明日目标 补全今天耽误的功课,继续学习java!
- day10设置文件权限
day10设置文件权限 yum复习 1.修改IP [root@localhost ~]# sed -i 's#.200#.50#g' /etc/sysconfig/network-scripts/if ...
- Ubuntu Linux安装QT5之旅
1. QT 版本选择 如何选择QT版本,参考如下介绍 https://www.cnblogs.com/chinasoft/p/15226293.html 2. 在此以5.15.0解说 下载QT 版本 ...
- Shell脚本字符串截取方法总结
Shell脚本8种字符串截取方法总结转自:https://www.cnblogs.com/ralphdc/p/8032335.html Linux 的字符串截取很有用.有八种方法.假设有变量 var= ...
- 利用unordered_map维护关联数据
在leetcode上刷339题Evaluate Division(https://leetcode.com/problems/evaluate-division/#/description)时在脑中过 ...
- get请求url参数中有+、空格、=、%、&、#等特殊符号的问题解决
url出现了有+,空格,/,?,%,#,&,=等特殊符号的时候,可能在服务器端无法获得正确的参数值,如何是好?解决办法将这些字符转化成服务器可以识别的字符,对应关系如下:URL字符转义 用其它 ...