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. 1 <= A.length <= 100
  2. 1 <= A[i].length <= 100
  3. 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的更多相关文章

  1. Find Common Characters - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Find Common Characters - LeetCode 注意点 不能单纯的以字母出现的次数来判断是否是公共的字母 解法 解法一:将第一个字符串 ...

  2. 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 ...

  3. LeetCode 1002. Find Common Characters (查找常用字符)

    题目标签:Array, Hash Table 题目给了我们一个string array A,让我们找到common characters. 建立一个26 size 的int common array, ...

  4. 【LEETCODE】43、1002. Find Common Characters

    package y2019.Algorithm.array; import java.util.*; /** * @ProjectName: cutter-point * @Package: y201 ...

  5. [Swift]LeetCode1002. 查找常用字符 | Find Common Characters

    Given an array A of strings made only from lowercase letters, return a list of all characters that s ...

  6. 1002. Find Common Characters

    Given an array A of strings made only from lowercase letters, return a list of all characters that s ...

  7. 【leetcode】1002. Find Common Characters

    题目如下: Given an array A of strings made only from lowercase letters, return a list of all characters ...

  8. [LC] 1002. Find Common Characters

    Given an array A of strings made only from lowercase letters, return a list of all characters that s ...

  9. 【LeetCode】1002. Find Common Characters 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

随机推荐

  1. ionic3安卓平台引用高德地图

    1.前置条件 第一步,注册高德开发者:第二步,去控制台创建应用:第三步,获取Key. 2.打开src目录下的index.html, 在head标签中添加以下代码,引入js: <script ty ...

  2. jenkines的工作区目录位置查找

    先找到jenkines的主目录 系统-系统配置 然后工作区在主目录的workspace文件夹里面

  3. django 多线程下载图片

    example1: from multiprocessing.dummy import Pool as ThreadPool #多线程 import time import urllib2 urls ...

  4. MII、GMII、RMII、SGMII、XGMII 接口区别

    MII即媒体独立接口,也叫介质无关接口.它是IEEE-802.3定义的以太网行业标准.它包括一个数据接口,以及一个MAC和PHY之间的管理接口(图1). 数据接口包括分别用于发送器和接收器的两条独立信 ...

  5. Kettle在windows下分布式集群的搭建

    集群的搭建 我这里用的是kettle7.1版本的 下载解压 我们打开kettle的安装目录,进入到data-integration->pwd目录,找到carte-config-master-80 ...

  6. rpc调用过程

    在openstack中,各个组件之间的调用遵循RESTful风格,而组件内部各服务之间的相互调用采用rpc远程调用,比如nova-conductor和nova-compute rpc原理: 首先了解什 ...

  7. delete content on the right of cursor, Mac

    delete content on the right of cursor, Mac It's not convenient to press Fn+delete to delete content ...

  8. list按照某个元素进行排序

    import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.u ...

  9. android toolbar使用记录

    1.打开Project structure,选择app modules,切换到Dependencies添加com.android.support.design.26.0.0.alpha1 2.在lay ...

  10. Python3执行DOS命令并截取其输出到一个列表字符串,同时写入一个文件

    #执行DOS命令并截取其输出到一个列表字符串,同时写入一个文件#这个功能很有用listing=os.popen('ipconfig').readlines()for i in listing: pri ...