[LC] 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"]
class Solution {
public List<String> commonChars(String[] A) {
List<String> res = new ArrayList<>();
if (A == null || A.length == 0) {
return res;
}
int[] lowerCaseArr = new int[26];
for (char c: A[0].toCharArray()) {
lowerCaseArr[c - 'a'] += 1;
}
for (int i = 1; i < A.length; i++) {
int[] curDict = new int[26];
for (char c: A[i].toCharArray()) {
curDict[c - 'a'] += 1;
}
for (int j = 0; j < lowerCaseArr.length; j++) {
lowerCaseArr[j] = Math.min(lowerCaseArr[j], curDict[j]);
}
}
for (int i = 0; i < lowerCaseArr.length; i++) {
while (lowerCaseArr[i] > 0) {
res.add(Character.toString((char)(i + 'a')));
lowerCaseArr[i] -= 1;
}
}
return res;
}
}
[LC] 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, ...
- 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
题目如下: Given an array A of strings made only from lowercase letters, return a list of all characters ...
- 【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 ...
随机推荐
- c 循环左移
char b[11] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'}; const int iShift = 4; for (int j = ...
- JS向固定数组中添加不重复元素并冒泡排序
向数组{7,20,12,6,25}中添加一个不重复的数字,然后按照从小到大的顺序排列 源代码: <!DOCTYPE html> <html> <head> < ...
- Vue.js 之 过渡动画
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- WOJ 1546 Maze 图论上的状态压缩DP
http://acm.whu.edu.cn/land/problem/detail?problem_id=1546 这个题目还是聪哥教的方法过的 首先搜索是必须的,而且通过搜索来缩点,这些应该要想到, ...
- potplayer记住播放进度
(右键——选项)F5——播放——记忆视频播放位置
- python-IDE使用
集成开发工具 集成开发环境(IDE,Integrated Development Environment ) VIM 经典的linux下的文本编辑器,只有小白和的牛会用这个 Emacs linux文本 ...
- jq切换选择项
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- tf调试函数
Tensorflow之调试(Debug)及打印变量 参考资料:https://wookayin.github.io/tensorflow-talk-debugging 几种常用方法: 1.通过Se ...
- 使用sklearn做特征工程
1 特征工程是什么? 有这么一句话在业界广泛流传:数据和特征决定了机器学习的上限,而模型和算法只是逼近这个上限而已.那特征工程到底是什么呢?顾名思义,其本质是一项工程活动,目的是最大限度地从原始数据中 ...
- php错误和异常的重定向
通过重定向错误或异常,我们可以更安全的显示错误信息,一般也用来记录错误和异常日志. 参数可以是全局函数名,也可以是类中的方法,非静态方法通过数组传递类名和方法名进去, 静态方法直接带命名空间和类名,看 ...