【HackerRank】Gem Stones
John has discovered various rocks. Each rock is composed of various elements, and each element is represented by a lowercase latin letter from 'a' to 'z'. An element can be present multiple times in a rock. An element is called a 'gem-element' if it occurs at least once in each of the rocks.
Given the list of rocks with their compositions, display the number of gem-elements that exist in those rocks.
Input Format
The first line consists of N, the number of rocks.
Each of the next N lines contain rocks' composition. Each composition consists of lowercase letters of English alphabet.
Output Format
Print the number gem-elements that exist in those rocks.
Constraints
1 ≤ N ≤ 100
Each composition consists of only small latin letters ('a'-'z').
1 ≤ Length of each composition ≤ 100
题解:
import java.io.*;
import java.util.*;
import java.math.*; public class Solution {
static int[] Gem_Stones(String[] ele){
int[] answer = new int[27];
for(int i = 0;i < ele.length;i++){
boolean[] has = new boolean[27];
for(int j =0;j < ele[i].length();j++){
char temp = ele[i].charAt(j);
if(!has[temp-'a']){
answer[temp-'a']++;
has[temp-'a'] = true;
}
}
}
return answer;
} public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t;
t = in.nextInt();
String[] strings= new String[t];
for(int i = 0;i < t;i++){
strings[i] = in.next();
}
int[] answer = Gem_Stones(strings);
int count = 0;
for(int i = 0;i < answer.length;i++)
if(answer[i]== t )
count++;
System.out.print(count);
}
}
最新文章
- Python学习 过程中零散知识点的总结
- 深入理解JavaScript定时机制和定时器注意问题
- 网页js生成当前年月日 星期
- 【转】(DT系列四)驱动加载中, 如何取得device tree中的属性
- Leetcode算法刷题:第100题 Same Tree
- [代码]Java后台推送消息到IOS前端
- CCF计算机认证——字符串匹配问题(运行都正确,为什么提交后只给50分?)
- dom4j和jaxp解析工具的
- 使用Fiddler伪造服务端返回数据,绕过软件试用期验证
- mxnet框架样本,使用C++接口
- webSocket通讯
- Elasticsearch安装详解
- 远离压力,提高效率——Getting things done读书笔记
- MyDAL - is null &;&; is not null 条件 使用
- JMeter_JDBC 性能测试
- qt 打包发布 获取dll
- Delphi7连接MySql数据库-DBGrid控件显示数据
- 牛客网Wannafly挑战赛25A 因子(数论 素因子分解)
- mybatis的批量更新实例
- 关于UIGestureRecognizerState