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. ByteToByte64String、Base64StringToBytes

    public string ByteToByte64String(byte[] bytes) { return Convert.ToBase64String(bytes); } public byte ...

  2. 【算法和数据结构】_14_小算法_Blank字符替换

    /* 本程序用来将输入的制表符替换为\t, 而将退格替换为\b, 将反斜杠替换为\\ */ #include <stdio.h> #include <stdlib.h> typ ...

  3. android selector shape 使用

    先上效果图 message_toolbar_left_bg_selector <?xml version="1.0" encoding="utf-8"?& ...

  4. XML和实体类之间相互转换(序列化和反序列化)

    我们需要在XML与实体类,DataTable,List之间进行转换,下面是XmlUtil类,该类来自网络并稍加修改. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  5. disabled和readonly区别

    disabled和readonly这两个属性有一些共同之处,比如都设为true,则form属性将不能被编辑,往往在写js代码的时候容易混合使用这两个属性,其实他们之间是有一定区别的: 如果一个输入项的 ...

  6. JS数据类型的判断

    在 ECMAScript 规范中,共定义了 7 种数据类型,分为 基本类型 和 引用类型 两大类,如下所示:    基本类型:String.Number.Boolean.Symbol.Undefine ...

  7. nobup 与 后台运行命令

    1. Linux进程状态:R (TASK_RUNNING),可执行状态&运行状态(在run_queue队列里的状态) 2. Linux进程状态:S (TASK_INTERRUPTIBLE),可 ...

  8. Permanent Space 和 Heap Space

      JVM堆内存 JVM堆内存分为2块:Permanent Space 和 Heap Space. Permanent 即 持久代(Permanent Generation),主要存放的是Java类定 ...

  9. VS2010 永久配置OpenCv2.4.9 及转换到COFF 期间失败:文件无效或损坏,解决方法

    1.下载OpenCv2.4.9(win pack):http://opencv.org/releases.html 下载完成后,进行解压(win7 64位系统) 2.环境配置,配置如下图所示: 找到p ...

  10. C++获取当前执行程序文件所在的全路径