1002. Find Common Characters
Given an array
Aof 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 <= 1001 <= A[i].length <= 100A[i][j]is a lowercase letter
Approach #1: Array. [Java]
class Solution {
public List<String> commonChars(String[] A) {
List<String> ret = new ArrayList<>();
int[] count = new int[26];
Arrays.fill(count, Integer.MAX_VALUE);
for (String a : A) {
int[] cnt = new int[26];
for (char c : a.toCharArray()) ++cnt[c-'a'];
for (int i = 0; i < 26; ++i) count[i] = Math.min(count[i], cnt[i]);
}
for (int i = 0; i < 26; ++i)
while (count[i]-- > 0)
ret.add(""+(char)(i + 'a'));
return ret;
}
}
Reference:
1002. Find Common Characters的更多相关文章
- 【LEETCODE】43、1002. Find Common Characters
package y2019.Algorithm.array; import java.util.*; /** * @ProjectName: cutter-point * @Package: y201 ...
- LeetCode 1002. Find Common Characters (查找常用字符)
题目标签:Array, Hash Table 题目给了我们一个string array A,让我们找到common characters. 建立一个26 size 的int common array, ...
- 【leetcode】1002. Find Common Characters
题目如下: Given an array A of strings made only from lowercase letters, return a list of all characters ...
- [LC] 1002. Find Common Characters
Given an array A of strings made only from lowercase letters, return a list of all characters that s ...
- 【LeetCode】1002. Find Common Characters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...
- 1002. Find Common Characters查找常用字符
参考:https://leetcode.com/problems/find-common-characters/discuss/247573/C%2B%2B-O(n)-or-O(1)-two-vect ...
- Leetcode 1002. Find Common Characters
python可重集合操作 class Solution(object): def commonChars(self, A): """ :type A: List[str] ...
- Find Common Characters - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Find Common Characters - LeetCode 注意点 不能单纯的以字母出现的次数来判断是否是公共的字母 解法 解法一:将第一个字符串 ...
- Write a program that gives count of common characters presented in an array of strings..(or array of
转自出处 Write a program that gives count of common characters presented in an array of strings..(or arr ...
随机推荐
- 使用第三方容器服务,自动化部署.Net Core
1.为什么用第三方,而不自建,有哪些第三方,最后实现的效果 a.尝试过自建,并成功了,但是很麻烦,要敲一堆命令,无法达到全自动化部署的要求. b.自建,就算用第三方的镜像包,感觉下载还是不快,不知道为 ...
- 关于bootstrap的认识
学习一个框架最好的方法当然就是去它的官网查看它的官方文档,看看官网是怎么描述这个框架的吧------Sleek, intuitive, and powerful front-end framework ...
- BZOJ2424 [HAOI2010]订货 - 费用流
题解 (非常裸的费用流 题意有一点表明不清: 该月卖出的商品可以不用算进仓库里面. 然后套上费用流模板 代码 #include<cstring> #include<queue> ...
- sublime的坑
- git保护进程
https://github.com/Mattiwatti/PPLKiller 去掉进程保护内核级
- Mybatis之拦截器原理(jdk动态代理优化版本)
在介绍Mybatis拦截器代码之前,我们先研究下jdk自带的动态代理及优化 其实动态代理也是一种设计模式...优于静态代理,同时动态代理我知道的有两种,一种是面向接口的jdk的代理,第二种是基于第三方 ...
- 2018.10.21 codeforces1071B. Minimum path(dp+贪心+bfs)
传送门 唉考试的时候写错了两个细节调了一个多小时根本没调出来. 下来又调了半个小时才过. 其实很简单. 我们先dpdpdp出最开始最多多少个连续的aaa. 然后对于没法继续连续下去的用贪心+bfsbf ...
- python实现base64算法加密
python本身有base64加密的模块,不过是用C写的,封装成了.so文件,无法查看源码,本着学习的心态,自己实现了一遍,算法 原理参考 浅谈Base64编码算法. 代码如下: # coding:u ...
- [ajax] quick double or multiple click ajax submit cause chrome explorer's error snatshot
快速点击ajax提交,引发的错误截图1: snapshot -2:
- 从SEQUENCE跳号说起
http://blog.csdn.net/agaric717/article/details/6690890 一个应用上线后发现一个使用SEQUENCE值来生成的主键经常出现断号,而且断号不是一两个, ...