算法笔试题(六):删除给定字符串中出现次数最多的字符

来源:互联网 发布:校园预防网络诈骗ppt 编辑:程序博客网 时间:2024/06/17 11:39

主要是利用了字符的ascii码,以ascii码作为索引。

import java.util.Scanner; public class DeleteMostChar {  public static void main(String[] args) {  // TODO Auto-generated method stub  Scanner s = new Scanner(System.in);  String str = s.nextLine();  System.out.println("删除前字符串为:"+str);  str=deleteMostChar(str);  System.out.println("删除后字符串为:"+str);  }  public static String  deleteMostChar(String str)  {  int[] asciis = new int[256];  int maxIndexAndChar = 0;  for(int i = 0; i < str.length();++i)      {         asciis[str.charAt(i)]++;         if(asciis[str.charAt(i)] > maxIndexAndChar)                   maxIndexAndChar = str.charAt(i);      }   return  str.replace(maxIndexAndChar,"");  } }


0 0