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 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
<pre name="code" class="java">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>();
for(int i=0; i < input.length(); i++) {
char ch = input.charAt(i);
if (!uniqueCharSet.contains(ch)) {
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;
}
}
Write a program that gives count of common characters presented in an array of strings..(or array of的更多相关文章
- Find Common Characters - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Find Common Characters - LeetCode 注意点 不能单纯的以字母出现的次数来判断是否是公共的字母 解法 解法一:将第一个字符串 ...
- LeetCode 1002. Find Common Characters (查找常用字符)
题目标签:Array, Hash Table 题目给了我们一个string array A,让我们找到common characters. 建立一个26 size 的int common array, ...
- 【LEETCODE】43、1002. Find Common Characters
package y2019.Algorithm.array; import java.util.*; /** * @ProjectName: cutter-point * @Package: y201 ...
- [Swift]LeetCode1002. 查找常用字符 | Find Common Characters
Given an array A of strings made only from lowercase letters, return a list of all characters that s ...
- Find Common Characters LT1002
Given an array A of strings made only from lowercase letters, return a list of all characters that s ...
- 1002. Find Common Characters
Given an array A of strings made only from lowercase letters, return a list of all characters that s ...
- 【leetcode】1002. Find Common Characters
题目如下: Given an array A of strings made only from lowercase letters, return a list of all characters ...
- LeetCode.1002-寻找共有字符(Find Common Characters)
这是悦乐书的第375次更新,第402篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第236题(顺位题号是1002).给定仅由小写字母组成的字符串A,返回列表中所有字符串都 ...
- leetcode@ [315/215] Count of Smaller Numbers After Self / Kth Largest Element in an Array (BST)
https://leetcode.com/problems/count-of-smaller-numbers-after-self/ You are given an integer array nu ...
随机推荐
- vue+VeeValidate 校验范围(部分校验,全部校验)
搜索很久,没有发现有关于vue+VeeValidate部分校验的.自己写一个. 主要是两个场景: 1. 校验范围内,所有的字段. 2. 校验全局所有字段.主要方法: 1.validate(fields ...
- 【thinking in java】ArrayList源码分析
简介 ArrayList底层是数组实现的,可以自增扩容的数组,此外它是非线程安全的,一般多用于单线程环境下(Vector是线程安全的,所以ArrayList 性能相对Vector 会好些) Array ...
- 零基础入门学习Python(26)--字典:当索引不好用时2
知识点 删除字典元素 能删单一的元素也能清空字典,清空只需一项操作. 显示删除一个字典用del命令,如下: >>> dict1 = {'a':1,'b':2,'c':3} >& ...
- JavaScript小技巧整理篇(非常全)
能够为大家提供这些简短而实用的JavaScript技巧来提高大家编程能力,这对于我来说是件很开心的事.每天仅花上不到2分钟的时间中,你将可 以读遍JavaScript这门可怕的语言所呈现给我们的特性: ...
- Error: Divergence detected in AMG solver: k
** Error: Divergence detected in AMG solver: k A:Since you were working on convergence issue from pa ...
- Symmetry
Description The figure shown on the left is left-right symmetric as it is possible to fold the sheet ...
- DAG模型(矩形嵌套)
推荐在线例题:http://acm.nyist.net/JudgeOnline/problem.php?pid=16 题摘: 矩形嵌套 时间限制:3000 ms | 内存限制:65535 KB 难 ...
- Java线程和多线程(一)——线程的基本概念
Java 线程是一个轻量级执行任务的处理单元.Java提供了Thread类来支持多线程,开发者在应用中可以创建多个线程来支持并发执行任务. 在应用中存在两种类型的线程,用户线程和守护线程.当我们启动应 ...
- 图论算法——最短路径Dijkstra,Floyd,Bellman Ford
算法名称 适用范围 算法过程 Dijkstra 无负权 从s开始,选择尚未完成的点中,distance最小的点,对其所有边进行松弛:直到所有结点都已完成 Bellman-Ford 可用有负权 依次对所 ...
- xtu summer individual 6 D - Checkposts
Checkposts Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Orig ...