当前位置: 移动技术网 > IT编程>开发语言>Java > Java求字符串中出现次数最多的字符串以及出现次数

Java求字符串中出现次数最多的字符串以及出现次数

2019年07月22日  | 移动技术网IT编程  | 我要评论

金山公司面试题:一个字符串中可能包含a~z中的多个字符,如有重复,如string data="aavzcadfdsfsdhshgwasdfasdf",求出现次数最多的那个字母及次数,如有多个重复的则都求出。

此题的解题思路如下:

引入treeset:通过集合快速找到所有出现过的字符串
引入arraylist:为了快速排序,再通过stringbuffer生成排序后的字符串
通过string的indexof方法和lastindexof方法来计算每个字符串出现的次数最大值
使用hashmap存储出现多的字符串和次数

代码如下:

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) { 
    /** 
     * 第一步: 
     *   使用treeset快速找到所有出现的字符串 
     *   将输入的字符串按升序排列 
     */ 
    //将string转换为字符数组 
    char[] chars=input.tochararray(); 
    arraylist<string> lists=new arraylist<string>(); 
    //treeset是一个有序集合,treeset中的元素将按照升序排列 
    //通过treeset的不重复性,快速找到所有出现的字符串 
    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);   
    //排序 
    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);   
    //将排序好的字符数组转换为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);   
     
    /** 
     * 第二步: 找出出现相同的字符并记录出现多少次 
     */ 
    //最多重复出现多少次 
    int max=0; 
    //重复出现的字符 
    string maxstring=""; 
    /*//重复出现的字符列表 
    arraylist<string> maxlist=new arraylist<string>();*/ 
    //用来保存出现最多的字符串和次数 
    hashmap<string, integer> hm=new hashmap<string, integer>(); 
    //将出现过的字符遍历 
    iterator<string> its=set.iterator(); 
    while (its.hasnext()) { 
      string os=its.next(); 
      //字符出现在排序后input中的第一次位置 
      int begin=input.indexof(os); 
      //字符出现在排序后input中的最后一次位置 
      int end=input.lastindexof(os); 
      //字符出现的次数 
      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("重复最多的字母是:"+enties.getkey()); 
        system.out.println("重复最多的次数是:"+enties.getvalue()); 
      } 
    } 
  } 
} 

运行结果如下:

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 

重复最多的字母是:n重复最多的次数是:4 

当有字符串重复的次数相同时,也可以将它们都打印出来。


public static void main(string[] args) { 
  string input = "abbcccddddeeeeeffffffaaaaabbb"; 
  new sorttest().dostring(input); 
} 

运行结果如下:

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 
重复最多的字母是:f重复最多的次数是:6 
重复最多的字母是:a重复最多的次数是:6 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网