Question: give you a string containing space and other punctuation marks. Calculate the most frequently used letters and the number of times the letter appears.
[Code ]:
Private static void totalTimes (String str ){
Char [] ch = str. toCharArray ();
Map <Character, Integer> timesMap = new HashMap <Character,
Integer> ();
For (int I = 0; I <ch. length; I ++ ){
If (ch [I]> = 65 & ch [I] <= 90) | (ch [I]> = 97
& Ch [I] <= 122 )){
Integer freq = timesMap. get (ch [I]);
TimesMap. put (ch [I], freq = null? Integer. valueOf (1): ++ freq );
}
}
Int charMaxIndex = Collections. max (timesMap. values ());
Int charMinIndex = Collections. min (timesMap. values ());
Set <Character> maxSet = new HashSet <Character> ();
Set <Character> minSet = new HashSet <Character> ();
For (Map. Entry <character, integer> entry: timesmap. entryset ()){
If (entry. getvalue (). Equals (charmaxindex )){
Maxset. Add (entry. getkey ());
}
If (entry. getvalue (). Equals (charminindex )){
Minset. Add (entry. getkey ());
}
}
System. Out. println ("the most frequently-seen letters:" + maxset );
System. Out. println ("occurrences:" + charmaxindex );
System. Out. println ("the smallest letter appears:" + minset );
System. Out. println ("occurrences:" + charminindex );
}
Of course, everyone except the above will have their own code! If the collection in Java cannot be used, it may be written on the array as follows:
- String STR =
"Hello wolrd wlllkdsfhksadfls? Sdfls SDF. pqyutgvaaaxzsdfs"
+
- "Lsdfj, ljsfd ajfdsak sfksjdfisfsdkfj lsdfjsidf jsafdalsjfs sfskdfjs"
;
- Int
[] StrCounts =
New
Int
[
255
];
- Int
BiggestCount =
0
;
- Char
BiggestCh =
0
;
- Char
Ch =
0
;
- Int
CurrentCount =
0
;
- For
(
Int
I = str. length ()-
1
; I> =
0
; I --){
- Ch = str. charAt (I );
- CurrentCount = ++ strCounts [ch];
- If
(CurrentCount> biggestCount ){
- BiggestCount = currentCount;
- BiggestCh = ch;
- }
- }
- System. out. println (biggestCh );
- System. out. println (biggestCount );