您的位置:首页 > 其它

删除给定字符串中出现次数最多的字符

2016-10-12 17:47 246 查看
主要是利用了字符的ascii码,以ascii码作为索引。

import java.util.Scanner;
public class DeleteMostChar {

public static void main(String[] args) {
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,"");

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐