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 <= 100
1 <= A[i].length <= 100
A[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 ...
随机推荐
- gogs 源码阅读笔记 001
gogs 源码阅读笔记 001 gogs项目相当不错,本笔记实际是基于gogs fork版本 git-122a66f. gitea (gitea版本由来)[https://blog.gitea.io/ ...
- Android 最简单的MVP案例;
随手撸个发出来: V:界面层 //界面层需要实现P.View方法,然后重写P.View中的方法:M层给的数据就在这些个方法的参数中: // 还要获取到P.Provide的实例,使用P.Provide去 ...
- windows共享文件夹权限设置
权限设置及更改,最好在右键属性里面, 在计算机管理,共享文件夹->共享里面修改,有时候会不生效. windows的凭据修改,在用户注销后才会生效.
- 调整iframe滚动条失效
1:<iframe scrolling="auto" frameborder="0" src="' + add + '" style= ...
- Mysql 和 SQLServer 使用SQL差异比较
查询前100条数据 #mysql ; #sqlserver * from table_name ; 从数据库.表 定位表 #mysql写法:库名.表名 select password from Inf ...
- 深度学习原理与框架-递归神经网络-RNN_exmaple(代码) 1.rnn.BasicLSTMCell(构造基本网络) 2.tf.nn.dynamic_rnn(执行rnn网络) 3.tf.expand_dim(增加输入数据的维度) 4.tf.tile(在某个维度上按照倍数进行平铺迭代) 5.tf.squeeze(去除维度上为1的维度)
1. rnn.BasicLSTMCell(num_hidden) # 构造单层的lstm网络结构 参数说明:num_hidden表示隐藏层的个数 2.tf.nn.dynamic_rnn(cell, ...
- spark-1
先测试搭好的spark集群: 本地模式测试: 在spark的目录下: ./bin/run-example SparkPi 10 --master local[2] 验证成功: 集群模式 Spark S ...
- python 列表 list的基本操作
一,Python 的列表数据类型包含更多的方法. 这里是所有的列表对象方法: list.append(x) 把一个元素添加到链表的结尾,相当于 a[len(a):] = [x] . list.exte ...
- git克隆远程仓库的时候断电了,使用git-fetch断点续传
今天下载tensorflow serving 模型,但是因为主机电源线太长了,不知是我自己搞的还是同事,断电了都, 网速捉急,下载了挺长时间的,一看,git clone 到中途竟然断电,不过查看,还好 ...
- Redis核心原理
Redis系统介绍: Redis的基础介绍与安装使用步骤:https://www.jianshu.com/p/2a23257af57b Redis的基础数据结构与使用:https://www.jian ...