Java finds the most frequent string in a string and the number of occurrences

  • 2020-06-19 10:10:41
  • OfStack

1 string may contain more than one character in a~z. If there is repetition, for example, String data="aavzcadfdsfsdhshgWasdfasdf", find the letter with the most occurrence and the number of times, if there are more than one repetition, find out.

The idea of solving this problem is as follows:

Introduce TreeSet: A collection to quickly find all the strings that have appeared
ArrayList is introduced: For quicksort, the sorted string is then generated from StringBuffer
The indexOf and lastIndexOf methods of String are used to calculate the maximum number of occurrences of each string
Use HashMap to store the occurrence of multiple strings and times

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) { 
    /** 
     *  The first 1 Step:  
     *    use TreeSet Quickly find all the strings that appear  
     *    Sort the input strings in ascending order  
     */ 
    // will String Convert to an array of characters  
    char[] chars=input.toCharArray(); 
    ArrayList<String> lists=new ArrayList<String>(); 
    //TreeSet is 1 An ordered set, TreeSet Elements in will be arranged in ascending order  
    // through TreeSet Non-repeatable, quickly find all the strings that appear  
    TreeSet<String> set=new TreeSet<String>(); 
    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);   
    // The sorting  
    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.out.println("lists= "+lists);   
    // Converts the sorted character array to StringBuffer 
    StringBuffer sb=new StringBuffer(); 
    for (int i = 0; i < lists.size(); i++) { 
      sb.append(lists.get(i)); 
    } 
    input=sb.toString();  
    //input= abcdeeggghlnnnnooppstttuy 
    System.out.println("input= "+input);   
     
    /** 
     *  The first 2 Step:   Find out how many times the same character appears and record how many times  
     */ 
    // What is the maximum number of repetitions  
    int max=0; 
    // A recurring character  
    String maxString=""; 
    /*// A list of recurring characters  
    ArrayList<String> maxList=new ArrayList<String>();*/ 
    // Used to hold the string and the number of occurrences  
    HashMap<String, Integer> hm=new HashMap<String, Integer>(); 
    // The characters that appear will be traversed  
    Iterator<String> its=set.iterator(); 
    while (its.hasNext()) { 
      String os=its.next(); 
      // Characters appear after the sort input The first of 1 Time position  
      int begin=input.indexOf(os); 
      // Characters appear after the sort input In the last 1 Time position  
      int end=input.lastIndexOf(os); 
      // The number of occurrences of a 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 letter is :"+enties.getKey()); 
        System.out.println(" The maximum number of repetitions :"+enties.getValue()); 
      } 
    } 
  } 
} 

The operation results 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 letter was n and the most repeated was :4

When there are strings repeated the same number of times, they can also be printed out.

Such as


public static void main(String[] args) { 
  String input = "abbcccddddeeeeeffffffaaaaabbb"; 
  new SortTest().doString(input); 
} 

The operation results 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 letter is :f The maximum number of repetitions :6 
 The most repeated letter is :a The maximum number of repetitions :6 

Related articles: