题目如下:
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
解题思路:创建一个char_count[26]数字,记录a-z每一个字符在所有字符串中出现的最少的次数,初始值为101。接下来遍历Input中的每个单词,并且把每个字符在这个单词出现的次数与char_count中对应字符的次数进行比较取较小值。Input遍历完成后,char_count中每个字符所对应的值即为公共的个数。
代码如下:
class Solution(object):
def commonChars(self, A):
"""
:type A: List[str]
:rtype: List[str]
"""
cl = [101] * 26 for word in A:
for char in range(97,97+26):
inx = char - ord('a')
cl[inx] = min(cl[inx],word.count(chr(char)))
res = []
for i in range(len(cl)):
res += [chr(i + ord('a'))] * cl[i]
return res
【leetcode】1002. Find Common Characters的更多相关文章
-
【LeetCode】1002. Find Common Characters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
-
【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
-
【LeetCode】158. Read N Characters Given Read4 II - Call multiple times
Difficulty: Hard More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...
-
【LeetCode】157. Read N Characters Given Read4
Difficulty: Easy More:[目录]LeetCode Java实现 Description The API: int read4(char *buf) reads 4 charact ...
-
【leetcode】1143. Longest Common Subsequence
题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A su ...
-
【LeetCode】157. Read N Characters Given Read4 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...
-
【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...
-
【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
-
【LeetCode】14. Longest Common Prefix 最长前缀子串
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...
随机推荐
-
20145213《信息安全系统设计基础》实验一 Linux开发环境的配置
北京电子科技学院(BESTI) 实 验 报 告 课程:信息安全系统设计基础 班级:1452 姓名: 黄亚奇 祁玮 学号:20145213 20145222 成绩: 指导教师:娄嘉鹏 实验日期:2016 ...
-
[总结] I/O输入,输出
I/O输入,输出第一:先判断到底是输入还是输出,站在程序的立场第二:判断是传递字节,还是字符,决定管道粗细,字节流是最基本的数据输出管道.字符类型管道专门用来传送文本数据.Java流的四大父类:1.字 ...
-
我的第一个GitHub仓库
GitHub 仓库地址 https://github.com/FBean/test.git GitHub 常用命令 add--Add file contents to the index bisect ...
-
I2C
1.I2C协议个. 2.1 I2CADR 地址寄存器CPU也可以是I2C的Slave,CPU的I2C地址有 I2CADR指定 2.2 I2CFDR 频率设置寄存器The serial bit cloc ...
-
和我一起学python,基本概念 (life is short ,we need python)
作者:tobecrazy 出处:http://www.cnblogs.com/tobecrazy 欢迎转载,转载请注明出处.thank you! 基本概念 : 常量: 常量名全部大写,如PI 变量: ...
-
解决T400\T500\W500等安装win10驱动后黑屏问题
T400.W500.T500等机型有双显卡的机型,在安装WIn10后会在驱动后黑屏,但可见启动画面: 原因:没有对应的双显卡驱动程序,导致系统无法正确识别显卡: 解决方法:开机按F1进入Bios,在显 ...
-
python之for学习
for i in range(100,-1,-1): print "%s\n"%i; import os path = 'D:\\Test' for root, ...
-
使用DialogFragment创建对话框总结
回调Activity中的函数 http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents 在DialogFragme ...
-
Checking the Calendar
Checking the Calendar time limit per test 1 second memory limit per test 256 megabytes input standar ...
-
PyCharm更换sublime类似主题
1. 下载jar主题包 下载地址:https://github.com/spasserby/PyCharm-monokai 2.导入pycharm设置 导入方法:file-->Import Se ...