当前位置:网站首页>Give a string ① please count the number of times each letter appears ② please print the pair with the most letters
Give a string ① please count the number of times each letter appears ② please print the pair with the most letters
2022-07-24 07:22:00 【Hand in your homework every day】
- According to the title , First of all, I thought of two methods . One is to use a hash table , The other is to use Map Collection to store < character , Number > The key/value pair .
The first method :
public class StringTest {
/* Set a static hash array , We all know that every letter corresponds to a askii Code value ,
Put alphabetic askII Code value as subscript , When traversing it , Its value is increased by one .
example :a Of ascii The code value is 65, When traversing it , take arr[65]++;*/
static int[] hash = new int[255];
public static void main(String[] args) {
String str = "ksjhfkjashfkahfkahfiuuytwurekahd";
// Traverse each character of the string
for(int i = 0;i<str.length();i++){
char c = str.charAt(i);
hash[c]++;
}
int max = 0;
// Find the maximum number of occurrences
for(int i:hash){
if(i>max)
max = i;
}
// When hash There is a value in the array equal to max, Then print the character corresponding to its subscript
for(int i = 0;i<hash.length;i++){
if(hash[i]==max){
System.out.println(Character.toChars(i));
}
}
}
}The second method :
public static void main(String[] args) {
String str = "ksjhfkjashfkahfkahfiuuytwurekahd";
HashMap<Character,Integer> hm = new HashMap<>();
Set<Character> charSet = hm.keySet();
/* Traversal string , If charSet There is this element , It's in hm Add the key value of this element 1;
If charSet There is no such element , Then take this value as the key , And assign its value to 1, Added to the hm in */
for(int i = 0;i<str.length();i++){
char c = str.charAt(i);
if(!charSet.contains(c)){
hm.put(c,1);
}else{
int num = hm.get(c);
num++;
hm.put(c,num);
}
}
// take hm Set the value of the key value pair of in a set and traverse to find the maximum value
int max = 0;
for(Integer i:hm.values()){
if(i>max)
max = i;
}
// Traverse HashMap The elements in , Set the value equal to max Key output of
Set<Map.Entry<Character, Integer>> sme = hm.entrySet();
for(Map.Entry<Character,Integer> me:sme){
if(me.getValue()==max){
System.out.println(me.getKey());
}
}
}
}
边栏推荐
- [line test] Figure finding regular questions
- fopen、fwrite、fseek、ftell、fread使用demo
- oracle中有A,B连个表,这两个表需要第三个表C关联,那怎么将A表中的字段MJ1更新为B表中MJ2的值
- Blockbuster live broadcast | orb-slam3 series code explanation map points (topic 2)
- 一个怎样的模式能让平台用户发生自助裂变?-链动2+1
- QoS quality of service 4 traffic regulation of QoS boundary behavior
- baddy:核心函数入口
- 二维平面多段线Y轴最短距离
- C language from entry to soil (II)
- Mongodb application scenario and model selection (massive data storage model selection)
猜你喜欢

What kind of mode can make platform users self-help fission- Chain 2+1

定制 or 通用,中国 SaaS 未来发展趋势是什么?

Development system selection route

Mongodb application scenario and model selection (massive data storage model selection)

周杰伦直播超654万人观看,总互动量破4.5亿,助力快手再破纪录

服务漏洞&FTP&RDP&SSH&rsync

Vulnhub DC1

Part I - Fundamentals of C language_ 11. Comprehensive project - greedy snake

Compilation and debugging (GCC, g++, GDB)

php 转义字符串
随机推荐
InjectFix原理学习(实现修复加法的热更)
论文阅读:HarDNet: A Low Memory Traffic Network
OpenCascade笔记:gp包
File "manage.py", line 14) from exc ^ syntaxerror: cause and solution of invalid syntax error
Single Gmv has increased 100 times. What is the "general rule" behind the rise of popular brands?
Chapter007 FPGA learning IIC bus EEPROM reading
[word] how to generate the index directory on the left
QoS quality of service 4 traffic regulation of QoS boundary behavior
Using depth and normal textures in unity
Talk about your thoughts about the future
编译与调试(gcc,g++,gdb)
Seminar 2022.07.22 -- Comparative learning
Learning notes - distributed transaction theory
【Word】如何生成左侧的索引目录
Vulnhub DC1
拉普拉斯(Laplace)分布
Opencascade notes: GP package
There are two tables in Oracle, a and B. these two tables need to be associated with the third table C. how to update the field MJ1 in table a to the value MJ2 in table B
AMD64(x86_64)架构abi文档:上
numpy.inf