Find Common Characters LT1002
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 <= 1001 <= A[i].length <= 100A[i][j]is a lowercase letter
Idea 1. build HashMap to count the occurence of each character, ('o' -> 2), loop each string in the array and build the array (used as HashMap), then scan the map, for each character, find out the minimul of the count and append the character as string to the array.
Time complexity: O(nm), n is the length of the array, m is the length of string, linear in terms of all charactes in the input array
Space complexity: O(26n)
class Solution {
public List<String> commonChars(String[] A) {
int n = A.length;
int[][] charCnt = new int[26][n];
for(int i = 0; i < n; ++i) {
for(int j = 0; j < A[i].length(); ++j) {
++charCnt[A[i].charAt(j) - 'a'][i];
}
}
List<String> result = new ArrayList<>();
for(int i = 0; i < 26; ++i) {
int count = Integer.MAX_VALUE;
for(int j = 0; j < n; ++j) {
count = Math.min(count, charCnt[i][j]);
}
while(count > 0) {
result.add(Character.toString((char)('a' + i)));
--count;
}
}
return result;
}
}
Idea 1.a save space by storing the minimal counts on the way
Time compexity: O(n*m) linear
Space complexity: O(1)
class Solution {
public List<String> commonChars(String[] A) {
int n = A.length;
int[] charCnt = new int[26];
int[] currCnt = new int[26];
Arrays.fill(charCnt, Integer.MAX_VALUE);
for(String str: A) {
Arrays.fill(currCnt, 0);
for(int j = 0; j < A[i].length(); ++j) {
++currCnt[str.charAt(j) - 'a'];
}
for(int j = 0; j < 26; ++j) {
charCnt[j] = Math.min(charCnt[j], currCnt[j]);
}
}
List<String> result = new ArrayList<>();
for(int i = 0; i < 26; ++i) {
for(int count = charCnt[i]; count > 0; --count) {
result.add(Character.toString((char)('a' + i)));
}
}
return result;
}
}
Find Common Characters LT1002的更多相关文章
- 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 ...
- LeetCode 1002. Find Common Characters (查找常用字符)
题目标签:Array, Hash Table 题目给了我们一个string array A,让我们找到common characters. 建立一个26 size 的int common array, ...
- 【LEETCODE】43、1002. Find Common Characters
package y2019.Algorithm.array; import java.util.*; /** * @ProjectName: cutter-point * @Package: y201 ...
- [Swift]LeetCode1002. 查找常用字符 | Find Common Characters
Given an array A of strings made only from lowercase letters, return a list of all characters that s ...
- 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 ...
- [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 ...
随机推荐
- 【python】numpy中的shape用法
转自 https://blog.csdn.net/u010758410/article/details/71554224# shape函数是numpy.core.fromnumeric中的函数,它的功 ...
- lavarel 中间件
创建中间件 php artisan make:policy UserPolicy 所有生成的授权策略文件都会被放置在 app/Policies 文件夹下. 让我们为默认生成的用户授权策略添加 upda ...
- cordova插件列表
主要来源为http://blog.csdn.net/github_39500961/article/details/76270299 1.获取当前应用的版本号 cordova plugin add c ...
- Vue 封装js 并 引用
/封装模块化文件 新建的.js文件 var storage = { set(key, value) { localStorage.setItem(key, JSON.stringify(value)) ...
- SpringBoot,SpringCloud入门到精通最简单教程
https://blog.csdn.net/ztx114/article/details/78091689
- 如何学习DeepLearning
多年来,科学家们为了搞清楚神经网络的运行机制,进行了无数次实验.但关于神经网络的内在运行方式,目前还没有系统性的理论,没有具体的路线可以指引你获得更好的性能.简单地下载开源工具包直接使用并不能跑出很棒 ...
- firewalld 操作
https://blog.csdn.net/s_p_j/article/details/80979450 firewall-cmd --permanent --add-rich-rule=" ...
- Python单元测试unittest【转自https://www.cnblogs.com/feng0815/p/8045850.html】
[转自https://www.cnblogs.com/feng0815/p/8045850.html] Python中有一个自带的单元测试框架是unittest模块,用它来做单元测试,它里面封装好了一 ...
- Python:Fintech产品的第一语言
来源商业新知,原标题:为什么说Python是Fintech与金融变革的秘密武器 人生苦短,不止程序员,Python正在吸引来自金融领域大佬们的青睐目光. 金融科技的风口下,无数传统金融人都想从中掘一桶 ...
- 源码解析之AQS源码解析
要理解Lock首先要理解AQS,而要理解并发类最好的方法是先理解其并发控制量不同值的含义以及该类运作流程,然后配合一步步看源码.该类有一个重要的控制量是WaitStates,节点的状态值. /** w ...