当前位置:网站首页>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());
}
}
}
}
边栏推荐
- Flow control statement of avascript
- Can recursion still play like this? Recursive implementation of minesweeping game
- 定制 or 通用,中国 SaaS 未来发展趋势是什么?
- Learning notes - distributed transaction theory
- 编译与调试(gcc,g++,gdb)
- QoS服务质量三DiffServ模型报文的标记及PHB
- Feature Selective Anchor-Free Module for Single-Shot Object Detection
- Notes on the basics of using parameters in libsvm (1)
- The goal you specified requires a project to execute but there is no POM in this directory
- 开发系统选择路线
猜你喜欢

MongoDB应用场景及选型(海量数据存储选型)

拉普拉斯(Laplace)分布

编译与调试(gcc,g++,gdb)

win10声音图标有个没有声音

Pytorch deep learning practice lesson 10 / assignment (basic CNN)

Take you step by step to learn C (one)

Customization or GM, what is the future development trend of SaaS in China?

一个怎样的模式能让平台用户发生自助裂变?-链动2+1

mysql查询当前节点的所有父级

第二部分—C语言提高篇_3. 指针强化
随机推荐
项目上线就炸,这谁受得了
野指针,空指针,失效指针
Write three piece chess in C language
Buddy: core function entry
Compilation and debugging (GCC, g++, GDB)
MongoDB应用场景及选型(海量数据存储选型)
cookie_ session
Bookkeeping app: xiaoha bookkeeping 2 - production of registration page
Part II - C language improvement_ 2. Memory partition
mysql查询当前节点的所有父级
Flow control statement of avascript
Nacos的高级部分
【时序逻辑电路】——寄存器
A great hymn
Opencascade notes: GP package
Take you step by step to learn C (one)
项目中数据库插入大批量数据遇到的问题
Development system selection route
MySQL语句
Use of redis