*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 are present in all 3 strings.

Note: The input strings contains only lower case alphabets

public int getNumOfCommonChars(String[] inputs) {
        // Return 0 if null / empty input or only one string is provided
        if(inputs == null || inputs.length < 2) {
            return 0;
        } else {
            //create an int array to hold # times character appears
            int[] charCounts = new int[256];
            for(String input : inputs) {
                Set<Character> uniqueCharSet = new HashSet<Character>(); //对于每个String,都新建一个HashSet
                for(int i=0; i < input.length(); i++) {
                    char ch = input.charAt(i);
                    if (!uniqueCharSet.contains(ch)) {  //没有charCounts才加,有就不加,用来保证一个String里重复的char不被数到。
                        uniqueCharSet.add(ch);
                        charCounts[(int) ch] += 1;
                    }
                }
            }

            int commonCharCount = 0;
            for (int i=0; i < 256; i++) {
                if (charCounts[i] == inputs.length()) {
                    commonCharCount++;
                }
            }

            return commonCharCount;
        }
    }

reference:

http://www.careercup.com/question?id=6283084039192576

时间: 2024-10-13 08:11:35

*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

(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] 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(查找常用字符)

1 #define pb push_back 2 #define maxSize 3939 3 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 4 5 const int N = 101; 6 class Solution 7 { 8 public: 9 vector<string> commonChars(vector<string>& A) 10 { 11 int hash[N][N]; 12 memset(ha

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&

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

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

为啥在java中不要使用char类型

背景 最近项目中遇到一个问题,反复测试才发现问题出在了数据库中,由于使用了 Hibernate 这种ORM框架,因此,在java中写的 EntityBean 就可以直接通过ORM映射到Oracle数据库了,这也导致了很多的问题.当然,查了很多的资料,最终解决了这个问题,并且对Oracle的数据类型也有了一个更深层次的理解.下面是我的译文(原文是英文版的). 译文 要理解char类型,您首先必须了解Unicode编码模式.Unicode的发明克服了传统的字符编码方案的局限性.在Unicode出现之