Jinshan Company Interview questions: A string may contain multiple characters in a~z, such as duplicate, such as String da ta= "Aavzcadfdsfsdhshgwasdfas DF ", to find the most frequently occurring letter and the number of times, if there are multiple repetitions are found.
The idea of solving this problem is as follows:
- Introducing TreeSet: Quickly find all occurrences of a string through a collection
- Introduction of ArrayList: To quickly sort, and then generate sorted strings by StringBuffer
- Calculates the maximum number of occurrences of each string by using the IndexOf method of string and the LastIndexOf method
- Multi- string and number of occurrences using HashMap storage
The code is as follows:
Import Java.util.arraylist;import java.util.collections;import Java.util.hashmap;import Java.util.Iterator;import Java.util.map;import Java.util.treeset;public class Sorttest {public static void main (string[] args) {String input = "http Blogcsdnnetouyangpeng "; new Sorttest (). dostring (input);} public void dostring (string input) {/** * First step: * Use TreeSet to quickly find all occurrences of the string * to sort the input strings in ascending order *///convert string to character array char[] Chars=inpu T.tochararray (); Arraylist<string> lists=new arraylist<string> ();//treeset is an ordered set, the elements in TreeSet will be sorted in ascending order//through TreeSet's non-repetition, Quickly find all occurrences of the string treeset<string> set=new treeset<string> (); for (int i = 0; i < chars.length; i++) {Lists.add (St Ring.valueof (Chars[i])); Set.add (String.valueof (chars[i]));} set= [A, B, C, D, E, G, H, L, N, O, p, s, T, U, y]system.out.println ("set=" +set); Sort Collections.sort (lists);//lists= [A, B, C, D, E, E, G, G, G, H, L, N, N, N, N, O, O, p, p, s, T, T, T, U, Y]system.ou T.println ("lists=" +lists); Converts a sorted array of characters to Stringbufferstringbuffer SB=new StringBuffer (); for (int i = 0; i < lists.size (); i++) {Sb.append (Lists.get (i));} Input=sb.tostring (); input= abcdeeggghlnnnnooppstttuySystem.out.println ("input=" +input); /** * Second step: Find out the same character and record how many times *///the maximum number of repetitions of the int max=0;//character String maxstring= "";/*//repeating character list arraylist<string > Maxlist=new arraylist<string> () *///used to save the most occurrences of the string and the number of times hashmap<string, integer> hm=new hashmap< String, integer> ();//The character that appears is traversed iterator<string> its=set.iterator (); while (Its.hasnext ()) {String os= Its.next ();///The character appears in the first position of the sorted input in int begin=input.indexof (OS);//characters appear at the last position in the sorted input int end=input.lastindexof (OS );//The number of occurrences of the character int value=end-begin+1;if (Value>=max) {max=value;maxstring=os;hm.put (maxstring, max);}} For (map.entry<string, integer> enties:hm.entrySet ()) {if (Enties.getvalue () ==max) {System.out.print ("the most repeated letters are : "+enties.getkey ()); System.out.println ("The maximum number of repetitions is:" +enties.getvalue ());}}}}
The results of the operation are as follows:
Set= [A, B, C, D, E, G, H, L, N, O, p, s, T, U, y]lists= [A, B, C, D, E, E, G, G, G, H, L, N, N, N, N, O, O, p, p, S, T, T, T, U, y]input= abcdeeggghlnnnnooppstttuy the most repeated letters are: n The maximum number of repetitions is: 4
They can also be printed when the number of string repetitions is the same. Such as
public static void Main (string[] args) {String input = "abbcccddddeeeeeffffffaaaaabbb"; new Sorttest (). dostring (input);}
The results of the operation are as follows:
Set= [A, B, C, D, E, f]lists= [A, A, a, a, a, a, B, B, B, B, B, C, C, C, D, D, D, D, E, E, E, E, E, F, F,, F, F, F, F]input= AAAAAABBBBBCCCDDDDEEEEEFFFFFF The most repeated letters are: F repeats the most number of times is: 6 The most repeated letters are: a repeat the maximum number of times is: 6
==================================================================================================
Ouyangpeng welcome reprint, sharing with people is the source of progress!
Reprint please keep the original address : Http://blog.csdn.net/ouyang_peng
==================================================================================================
My Java Development learning journey------> Find the most frequently occurring string in a string and the number of occurrences