My Java development and learning journey ------ & gt; find the string that appears the most frequently and the number of occurrences in the string ------

Source: Internet
Author: User

My Java development and learning journey ------> Find the string that appears the most frequently and the number of times it appears ------

Kingsoft company interview questions: a string may contain ~ Multiple characters in z, such as String data = "aavzcadfdsfsdhshgWasdfasDf ", find the letter and number of occurrences that appear the most frequently. If there are multiple duplicates, find them.



The solution is as follows:


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 = "httpblogcsdnnetouyangpeng"; new SortTest (). doString (input);} public void doString (String input) {/*** Step 1: * use TreeSet to quickly find all the strings that appear * sort the input strings in ascending order * // convert the String to the character array char [] chars = input. toCharArray (); ArrayList <String> lists = new ArrayList <String> (); // The TreeSet is an ordered set, elements in the TreeSet will be sorted in ascending order // you can quickly find all the strings that appear in the TreeSet <String> set = new TreeSet <String> () through the non-repetition of the TreeSet (); for (int I = 0; I <chars. length; I ++) {lists. add (String. 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, h, l, n, o, o, p, p, s, t, u, y] System. out. println ("lists =" + lists); // convert the sorted character array 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);/*** Step 2: find the occurrence of the same character and record the number of times * // The maximum number of times that a duplicate occurs int max = 0; // The repeated character String maxString = ""; /* // repeated Character List ArrayList <String> maxList = new ArrayList <String> (); * // It is used to save the most frequently occurring strings and times HashMap <String, integer> hm = new HashMap <String, Integer> (); // traverses Iterator <String> its = set. iterator (); while (its. hasNext () {String OS = its. next (); // The first position where the character appears in the sorted input. int begin = input. indexOf (OS); // the last position where the character appears in the sorted input int end = input. lastIndexOf (OS); // number of occurrences of characters 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 most repeated times are:" + enties. getValue ());}}}}

The running result is 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, h, l, n, o, o, p, p, s, t, t, t, u, y] input = abcdeeggghlnnnnooppstttuy the most repeated letters are: n the most repeated times are: 4

When the number of string duplicates is the same, you can print them all. For example
public static void main(String[] args) {String input = "abbcccddddeeeeeffffffaaaaabbb";new SortTest().doString(input);}
The running result is as follows:
Set = [a, B, c, d, e, f] lists = [a, B, c, d, e, f, f] input = aaaaaabbbbbcccddddeeeeeffffff the most repeated letters are: f the most repeated letters are: 6 the most repeated letters are: a most repeated times are: 6

========================================================== ========================================================== ============================

Author: Ouyang Peng: Welcome to repost. sharing with others is the source of progress!

Reprinted Please retain the original address: http://blog.csdn.net/ouyang_peng

========================================================== ========================================================== ============================


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.