[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 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"]
class Solution {
    public List<String> commonChars(String[] A) {
        List<String> res = new ArrayList<>();
        if (A == null || A.length == 0) {
            return res;
        }
        int[] lowerCaseArr = new int[26];
        for (char c: A[0].toCharArray()) {
            lowerCaseArr[c - ‘a‘] += 1;
        }
        for (int i = 1; i < A.length; i++) {
            int[] curDict = new int[26];
            for (char c: A[i].toCharArray()) {
                curDict[c - ‘a‘] += 1;
            }
            for (int j = 0; j < lowerCaseArr.length; j++) {
                lowerCaseArr[j] = Math.min(lowerCaseArr[j], curDict[j]);
            }
        }

        for (int i = 0; i < lowerCaseArr.length; i++) {
            while (lowerCaseArr[i] > 0) {
                res.add(Character.toString((char)(i + ‘a‘)));
                lowerCaseArr[i] -= 1;
            }
        }
        return res;
    }
}

原文地址:https://www.cnblogs.com/xuanlu/p/12208573.html

时间: 2024-11-23 16:26:32

[LC] 1002. Find Common Characters的相关文章

【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

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

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

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

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

LC.235.Lowest Common Ancestor of a Binary Search Tree

https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: "The lowe

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

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

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

[LC] 14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". Example 1: Input: ["flower","flow","flight"] Output: "fl" Exa