1002. Find Common Characters - Easy

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

use hash table

time: O(n), space = O(n)

class Solution {
    public List<String> commonChars(String[] A) {
        int[] countMin = new int[26];
        Arrays.fill(countMin, Integer.MAX_VALUE);

        for(String s : A) {
            int[] cnt = new int[26];
            for(int i = 0; i < s.length(); i++) {
                cnt[s.charAt(i) - ‘a‘]++;
            }
            for(int i = 0; i < 26; i++) {
                countMin[i] = Math.min(cnt[i], countMin[i]);
            }
        }

        List<String> res = new ArrayList<>();
        for(char c = ‘a‘; c <= ‘z‘; c++) {
            while(countMin[c - ‘a‘] > 0) {
                res.add(String.valueOf(c));
                countMin[c - ‘a‘]--;
            }
        }

        return res;
    }
}

原文地址:https://www.cnblogs.com/fatttcat/p/11330423.html

时间: 2024-07-30 17:26:12

1002. Find Common Characters - Easy的相关文章

【Golang语言】LeetCode 1002. Find Common Characters

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 i

[LC] 1002. Find Common Characters

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

Leetcode 1002. Find Common Characters

python可重集合操作 class Solution(object): def commonChars(self, A): """ :type A: List[str] :rtype: List[str] """ if not A: return [] from collections import Counter ans=Counter(A[0]) for str in A: ans&=Counter(str) ans=list(an

*Common characters

Write a program that gives count of common characters presented in an array of strings..(or array of character arrays) For eg.. for the following input strings.. aghkafgklt dfghako qwemnaarkf The output should be 3. because the characters a, f and k

(Easy) Find Common Characters LeetCode

Description 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,

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

目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表.例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次. 你可以按任意顺序返回答案. 示例 1: 输入:["bella","label","roller"] 输出:["e","l","l&

LeetCode.1002-寻找共有字符(Find Common Characters)

这是悦乐书的第375次更新,第402篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第236题(顺位题号是1002).给定仅由小写字母组成的字符串A,返回列表中所有字符串都有显示的字符的列表(包括重复字符).例如,如果一个字符在所有字符串中出现3次但不是4次,则需要在最终答案中包含该字符三次. 你可以按任何顺序返回答案.例如: 输入:["bella","label","roller"] 输出:["e"

14. Longest Common Prefix [easy] (Python)

题目链接 https://leetcode.com/problems/longest-common-prefix/ 题目原文 Write a function to find the longest common prefix string amongst an array of strings. 题目翻译 写个函数,找出一个字符串数组中所有字符串的最长公共前缀. 题目描述不清晰...补充几个例子,比如: {"a","a","b"} 返回 &qu

[LeetCode] 014. Longest Common Prefix (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 014.Longest_Common_Prefix (Easy) 链接: 题目:https://oj.leetcode.com/problems/longest-common-prefix/ 代码(github):https://github.com/illuz/leetcode 题意: 求多个字符串的最长公共前缀