【leetcode】1002. 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
解题思路:创建一个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. 思路:求最长前缀子 ...
随机推荐
- 测试md代码折叠功能
展开查看 System.out.println("Hello to see U!");
- 第三周作业—N42-虚怀若谷
一.显示/etc/目录下,以非字母开头,后面跟了一个字母以及其它任意长度任意字符的文件或目录 ls /etc/[^[:alpha:]][[:alpha:]]* 二.复制/etc/目录下所有以p开头的 ...
- 命令查看当前运行APP的包名和Activity
先用usb链接手机 启动要查看的程序,命令查看当前运行的包名和Activity更清晰. 使用adb shell dumpsys window | findstr mCurrentFocus 命令查看 ...
- IntelliJ IDEA设置maven
1.更改默认的maven仓库 2.手动更新maven 项目——也就是下载依赖的jar包 3. 不想每次手动更新,设置IDEA自动更新mav项目,下载jar包
- Java Web学习总结(12)Filter过滤器
一,Filter简介 Filter也称之为过滤器,Filter是对客户端访问资源的过滤,符合条件放行,不符合条件不放行,并且可以对目标资源访问前后进行逻辑处理. 二,Filter开发步骤 1)编写一个 ...
- Hadoop编程调用HDFS(JAVA)
1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 Hadoop环境: Cloudera QuickStart 2.GITHUB地址 http ...
- kNN#约会网站预测数据
#约会网站预测数据 def classifyPersion(): resultList = ['not at all','in small doses','in large doses'] #inpu ...
- 回复git@vger.kernel.org的注意事项
比如回复这封邮件 https://public-inbox.org/git/db2dcf54-8b1c-39b1-579c-425ef158c6a1@kdbg.org/ Reply instructi ...
- Ubuntu紫色背景颜色代码
前言 我一直很中意Ubuntu的紫,记录一下颜色代码以免忘了! so Ubuntu紫色背景颜色代码background=300924 red = 48 green 9 blue 36
- Mac定时执行脚本_服务launchctl
Mac 设置自动执行定时任务, 步骤: 1. 编写plist 2.将plist放入该目录下 ~/Library/LaunchAgents 3.命令启动 添加: launchctl load /Syst ...