【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. 思路:求最长前缀子 ...
随机推荐
- Delphi Win API 函数 [ ShellAPI ] ShellExecute 函数
引用单元:uses ShellAPI; 函数原型:function ShellExecute(hWnd: HWND; Operation, FileName, Parameters,Directory ...
- APP前置代码
APP自动化前置代码: #导入包from appium import webdriverimport timedesired_caps = {}desired_caps['platformName'] ...
- mybatis的Date类型。
在写select的时候,里面的查询语句.where后面如果jdbcType=DATE没有写的话是 这个形式的. <select id="selectPhoto" parame ...
- ldd3 编写scull尝试
快速参考: #include <linux/types.h> dev_t dev_t is the type used to represent device numbers within ...
- Kattis - gcpc (treap模板)
ne hundred years from now, in 21172117, the International Collegiate Programming Contest (of which t ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 GSM Base Station Identification (点在多边形内模板)
In the Personal Communication Service systems such as GSM (Global System for Mobile Communications), ...
- SaltStack(自动化运维工具)
SaltStack管理工具允许管理员对多个操作系统创建一个一致的管理系统,包括VMware vSphere环境.SaltStack作用于仆从和主拓扑.SaltStack与特定的命令结合使用可以在一个或 ...
- Critical Links
UVA 796 Critical Links http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82833#overview 题目大意:给你一 ...
- JS继承 实现方式
JS中继承方式的实现有多种方法,下面是比较推荐的方法,其它继承方式可做了解: function object(o) { function F() {} F.prototype = o; return ...
- Windows 08 R2_创建AD DS域服务(图文详解)
目录 目录 Active Directory概念 创建第一个AD域控制器 搭建DNS服务器 使用Windows窗口程序创建AD域控制器 AD与LDAP的关系 使用Powershell来创建ADDS域控 ...