题目如下:

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

解题思路:创建一个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的更多相关文章

  1. 【LeetCode】1002. Find Common Characters 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  2. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】158. Read N Characters Given Read4 II - Call multiple times

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description Similar to Question [Read N Characters Given ...

  4. 【LeetCode】157. Read N Characters Given Read4

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description The API: int read4(char *buf) reads 4 charact ...

  5. 【leetcode】1143. Longest Common Subsequence

    题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A su ...

  6. 【LeetCode】157. Read N Characters Given Read4 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 直接调用 日期 题目地址:https://leetco ...

  7. 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...

  8. 【LeetCode】14. Longest Common Prefix 最长公共前缀

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...

  9. 【LeetCode】14. Longest Common Prefix 最长前缀子串

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...

随机推荐

  1. C# 枚举的声名和使用

    namespace xxxxxx { public enum EnumTextHAlign { Left = , Center = , Right = } } using xxxxxx;

  2. Kafka长文总结

    Kafka是目前使用较多的消息队列,以高吞吐量得到广泛使用 特点: 1.同时为发布和订阅提供搞吞吐量.Kafka的设计目标是以时间复杂度为O(1)的方式提供消息持久化能力的,即使对TB级别以上数据也能 ...

  3. MySQL索引优化之双表示例

    select * from tableA a left join tableB b on a.f_id = b.id; 索引建tableB表上面, 因为left join 注定左表全都有,所以应该关心 ...

  4. Activity 启动模式总结

    Activity 启动模式: 1. standard: 默认启动模式,每次启动一个Activity都会重新创建一个实例: 2. singleTop: 栈顶复用模式,新Activity位于任务栈的栈顶, ...

  5. LOJ 2302 「NOI2017」整数——压位线段树

    题目:https://loj.ac/problem/2302 压30位,a最多落在两个位置上,拆成两次操作. 该位置加了 a 之后,如果要进位或者借位,查询一下连续一段 0 / 1 ,修改掉,再在含有 ...

  6. flask-路转换器

    from flask import Flask, render_template from werkzeug.routing import BaseConverter # 配置regex路由转换器 # ...

  7. 高并发大流量专题---3、前端优化(减少HTTP请求次数)

    高并发大流量专题---3.前端优化(减少HTTP请求次数) 一.总结 一句话总结: 图片地图:使用<map><area></area></map>标签. ...

  8. openoffice+pdf2swf+FlexPaper在线显示office和pdf

    前提:本人的系统为Ubuntu 13.10 64位系统.本篇是我在配置好环境后一段时间写的,所以操作上可能会有也错误,因此仅供参考. 搜索在线显示office和pdf,最常见的方法就是把都转为swf, ...

  9. Bootstarp-源码分析-alert.js v3.x和v4.x的对比

    一些概念 1. 使用 data-api 调用 就是给所有带有data-dismiss="alert"的元素绑定点击事件 v3.x: $(document).on('click.bs ...

  10. 使用Excel绘制F分布概率密度函数图表

    使用Excel绘制F分布概率密度函数图表 利用Excel绘制t分布的概率密度函数的相同方式,可以绘制F分布的概率密度函数图表. F分布的概率密度函数如下图所示: 其中:μ为分子自由度,ν为分母自由度 ...