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 ...
随机推荐
- Java遍历Map对象的方式
public static void main(String[] args) { HashMap<String, String> testMap = new HashMap<> ...
- Android批量验证渠道、版本号(Linux版)
功能:可校验单个或目录下所有apk文件的渠道号.版本号(Linux版本)使用说明:1.copy需要校验的apk文件到VerifyChannelVersion目录下2../VerifyChannelVe ...
- 树莓派apt-get The value '\stable' is invalid for APT 错误
对apt-get进行任何操作都会报错: pi@raspberrypi:~ $ sudo apt-get upgrade Reading package lists... Done E: The val ...
- Linux查看端口占用情况并释放端口占用
1.netstat -tunlp:查看所有tcp/udp端口占用及进程相关信息 2.netstat -tln | grep 端口号:查看特定端口占用情况 3.kill -9 进程ID(PID):释放指 ...
- 使用 tag 文件定义自定义标签
----------------------------------------------------------------------- 在jsp文件中,可以引用tag和tld文件. 1.对于t ...
- [UE4]Native Widget Host
一.Native Widget Host是一个容器,它可以包含一个Slate UI 二.Native Widget Host应该用在当你需要把一个Slate UI 放到UMG中的时候,只有这个时候才需 ...
- superrvisor application config ini
1. zookeeper [program:zookeeper] environment = JAVA_HOME="/opt/jdk1.8.0_191" process_name= ...
- xsy子矩形
考虑一种解题方法,枚举上下边界L,R, 然后二分答案T,我们要判断的是否存在 \[ \frac{(sum_j - sum_i)}{2 * (R - L + 1 + j - i)} \ge T \] 也 ...
- python3:实现字符串的全排列(有重复字符)
抛出问题 求任意一个字符串的全排列组合,例如a='123',输出 123,132,213,231,312,321. 解决方案 #字符串任意两个位置字符交换 def str_replace(str, x ...
- C#各种小问题汇总不断更新
IIS Express Worker Process已停止工作-->管理员身份运行CMD 输入netsh winsock reset 回车OK 未能从程序集“System.ServiceMode ...