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

Accepted

33,870

Submissions

51,601

Solution

class Solution {
    public List<String> commonChars(String[] A) {

        if(A ==null || A.length==0){
            return null;
        }

        List<String> list = new ArrayList<String>();

        for(int i = 0; i< A[0].length(); i++){

            Character ch = A[0].charAt(i);

            int count = Count(A[0], ch);

            boolean flag = true;

            for(int j = 1; j<A.length; j++)
            {

                    if(!check(A[j],ch)){

                        flag = false;
                    }

                count = count> Count(A[j],ch)? Count(A[j],ch): count;

            }

            if(flag &&!list.contains(Character.toString(ch)) ){
               for(int t =0; t<count;t++){
                   list.add(Character.toString(ch));
               }
            }
        }

        return list;

    }

    public boolean check(String str, Character ch){

        for(int i = 0; i<str.length();i++){

            if(str.charAt(i)== ch){
                return true;
            }
        }

        return false;
    }

    public int Count(String str, Character ch){

        int count = 0;
        for(int i = 0; i<str.length();i++){
            if(str.charAt(i)==ch){
                count++;
            }
        }
        return count;
    }

}

原文地址:https://www.cnblogs.com/codingyangmao/p/11387420.html

时间: 2024-10-02 19:00:28

(Easy) Find Common Characters LeetCode的相关文章

(Easy) Most Common Word - LeetCode

Description: Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words.  It is guaranteed there is at least one word that isn't banned, and that the answer is unique. Words in the list of bann

*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

Longest Substring Without Repeating Characters leetcode java

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s

Longest Common Prefix leetcode java

题目: Write a function to find the longest common prefix string amongst an array of strings. 题解: 解题思路是,先对整个String数组预处理一下,求一个最小长度(最长前缀肯定不能大于最小长度). 然后以第0个字符串作为参照,从第1个字符串到最后一个字符串,对同一位置做判断,有不同字符串返回当前记录的字符串就行. 我的代码如下,不是那么简洁好看,下面有个整理的更好一些: 1     private stat

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

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

【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

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

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