如何统计给定字符串S中出现最多的字符?

zjh53311 2007-12-27 08:03:18
如何统计给定字符串S中出现最多的字符? 谢谢 请帮帮忙!
...全文
682 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
头发健在吗 2010-08-06
  • 打赏
  • 举报
回复
public class StringTest3 {

private static final String StringBuffer = null;

public static void main(String[] args) {
String s = "1;a1afasdfasdffajfdlfkajdlf";
StringBuffer sb = new StringBuffer(s);
System.out.println(bubbleSort(sb));
findStr(sb);
// System.out.println(sb.charAt(1));
//按数字 符号 英文字母 的顺序来排列的
// System.out.println(sb.charAt(0)>sb.charAt(1));
}
//找出最多的字符
private static void findStr(StringBuffer sb) {
int maxCount=1;//记录出现最多的字符的次数
int count =1;//记录当前字符出现得次数
char maxStr =sb.charAt(0);//记录最多的字符
char currStr =sb.charAt(0);//记录当前出现的字符
char prevStr =sb.charAt(0);//上一个字符串
for(int i =0;i<sb.length();i++){
currStr = sb.charAt(i);
if(currStr==prevStr){
count++;

}else{
if(count>maxCount){
maxStr =prevStr;
maxCount=count;
}
count = 1;
prevStr = currStr;
}
}
System.out.println(maxStr);
System.out.println(maxCount);

}

// 冒泡排序分类
private static StringBuffer bubbleSort(StringBuffer sb) {
char temp;
for (int j = sb.length() - 1; j > 0; j--) {

for (int i = 0; i < j; i++) {
if (sb.charAt(i) > sb.charAt(i + 1)) {
temp = sb.charAt(i);
sb.setCharAt(i, sb.charAt(i + 1));
sb.setCharAt(i + 1, temp);
}
}

}
return sb;
}

}
you7363 2007-12-29
  • 打赏
  • 举报
回复
class test1 {
public static void main (String[] args)
{
String str="abcabaa";
char [] ch=str.toCharArray();
int max=0;
char c=0;
for (int i = 0; i<str.length(); i++)
{
int x=0;
for (int j = 1; j<str.length(); j++)
{

if(ch[i]==ch[j])
x++;
}
if(x>max)
{ c=ch[i];
max=x;
}

}
System.out.print(c+"出现"+(max+1)+"次");
}
}
for_cyan 2007-12-28
  • 打赏
  • 举报
回复
11楼方法好,用空间换效率啊,O(n)的级别
Kreocn 2007-12-28
  • 打赏
  • 举报
回复
楼上的方法不错......

不过.判断大小还是用
Math.max(counts[ch],counts[max])
比较好,,比if要快
不懂编程 2007-12-28
  • 打赏
  • 举报
回复
前几天在网上看到别人的代码,觉得很不错,希望对LZ有帮助

String str = "aabcwdsfwewefwfewfeweweggffffff";
char[] arr = str.toCharArray();
int[] counts = new int[128]; // 以字符的ASCII码为索引

char max = 0;
for (char ch : arr) {
counts[ch]++;
System.out.println(counts[ch]);
System.out.println(counts[max]);
if (counts[ch] > counts[max])
max = ch;
}
System.out.println("char=" + max + " count=" + counts[max]);
oliveyuganfei 2007-12-28
  • 打赏
  • 举报
回复
我写了一个.
public class TotalNumber {
public static String s="dfddddddfeedddrerer";
public static void main(String[] args){
System.out.println(search(s));
}
public static String search(String s){
int[] num=new int[s.length()];
int max_count=0,max_id=0;
for(int i=0;i<s.length();i++){
for(int j=i;j<s.length();j++){
if(s.charAt(i)==s.charAt(j))num[i]++;
}
if(num[i]>max_count){
max_id=i;
max_count=num[i];
}
}
return s.charAt(max_id)+":"+max_count;
}
}
friday5pm 2007-12-28
  • 打赏
  • 举报
回复
public String getMaxCountChar(String str){}

friday5pm 2007-12-28
  • 打赏
  • 举报
回复
3楼应该是对的
梅小西Echo 2007-12-28
  • 打赏
  • 举报
回复

//在给定的字符串中,按照出现次数的多少重新排列,结果中不含重复的字符

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.TreeSet;


public class JudgeCharTimes
{

public static String getMaxString(String s) //找出给定字符串中重复次数最多的字符
{
char[] ch = s.toCharArray(); //转换成字符数组
ArrayList lists = new ArrayList();
TreeSet set = new TreeSet();
for( int i = 0; i < ch.length; i++ )
{
lists.add( String.valueOf( ch[i] ) ); //添加到lists
set.add( String.valueOf( ch[i] ) ); //添加去重复后的字符到set
}
// System.out.println("共有如下字符:" + set);
Collections.sort( lists ); //对字符进行排序
// System.out.println( "排序后为:" + lists );
StringBuffer sb = new StringBuffer();
for( int i = 0; i < lists.size(); i++ )
{
sb.append( lists.get( i ) );
}

s = sb.toString();
int maxCount = 0; //次数最多的字符的次数
String maxString = "";
ArrayList array = new ArrayList();

for( int i = 1; i < 3; i++ )
{
Iterator it = set.iterator(); //使用迭代器
while( it.hasNext() )
{
String o = (String)it.next();
int begin = s.indexOf( o );
int end = s.lastIndexOf( o );
int value = end - begin + 1;
if( value > maxCount )
{
maxCount = value;
maxString = o; //此时的o为最大次数的字符
}
}

}

return maxString ;
// return maxString +" " + maxCount; //字符和其次数
}
//----------------------------------------------------------------------------
public static String deleteElement(String s, String max) //删除给定字符串中给定的字符
{
ArrayList list = new ArrayList();

for( int i = 0; i < s.length(); i++ )
{
list.add( s.charAt( i ) ); //添加到list
}
Collections.sort(list);
StringBuffer sb = new StringBuffer();
for( int i = 0; i < list.size(); i++ )
{
sb.append( list.get( i ) );
}
sb.delete( sb.indexOf(max), sb.lastIndexOf(max) + 1 );
return sb.toString();
}
//-------------------------------------------------------------------------
public static String getResult(String s)
{
StringBuffer sb = new StringBuffer();

while( !s.equals( "" ))
{
sb.append( getMaxString( s ));
s = deleteElement( s, getMaxString( s ) );
}
return sb.toString();
}
// ----------------------------------------------------------------------------
public static void main(String[] args)
{
String s = "aasrtfgsadddppoyiutrjgnhgggdfffsdderrrruuubnvmcccvv";
// System.out.println( getResult( s ) ); //返回按次数从大到小的序列,去掉重复
System.out.println( getMaxString( s ) ); //返回最大字符和其次数
}


}

hanye2006 2007-12-28
  • 打赏
  • 举报
回复
楼主的意思是找出一个字符串中出现次数最多的字符吧?
应该不是判断字符串中字符"s"出现的次数吧
Kinglight 2007-12-28
  • 打赏
  • 举报
回复
public int findS(String str){
int count=0;
for(int i=0;i <str.length;i++)
if(str.charAt(i)=='S')count++;
return count;
}


这个方法OK
只爱西瓜 2007-12-28
  • 打赏
  • 举报
回复
public static void main(String[] args) throws Exception{
// TODO 自动生成方法存根
String s = "hasjdlkfjlasjd;fj";
HashMap<Character,Integer> hs= new HashMap<Character,Integer>();
//依次取出每个字符,存在一个HashMap中,如果已经存在存过,次数+1
Character ch=null;
for(int i=0;i<s.length();i++) {
ch = Character.valueOf(s.charAt(i));
Integer num = hs.get(ch);
hs.put(ch,num == null ? 1 :num.intValue() + 1);
}

//遍历HashMap,找出出现次数最多的字符
System.out.println(ch+" "+hs.get(ch));
这样就可以得到了
baobao28 2007-12-28
  • 打赏
  • 举报
回复
我告诉你个更简单的方法吧

String s="abc,afajsdlkfjalkjfklajdf ajdsfklajsdf;ljqweklrjqwelkr;j";
String[] ss=s.split("s");
System.out.println(ss.length-1);

int j=0;
for (int i=0;i<s.length();i++){
if (s.substring(i,i+1).equals("s")){
j++;
}
}
System.out.println(j);

上面的方法如果想要严谨一些,那么就判断一下s是不失首和末
urumi 2007-12-28
  • 打赏
  • 举报
回复
很多人都没有看懂题目啊,还代码贴的一身劲~~
Bitsbase 2007-12-27
  • 打赏
  • 举报
回复
有没有相应的函数啊,不知道,查一下呢?
记得有查看第多少位是什么字符的函数的,可以调用一下,比较一下,、、、、、
hanye2006 2007-12-27
  • 打赏
  • 举报
回复
我也是初学
下面这个程序可以实现
只是写的不够专业
希望对你能有帮助


import java.util.*;
public class Test {
/*统计给定字符串S中出现最多的字符
思路:
用HashMap存储每个字符和其出现次数
然后遍历HashMap找出出现次数最多的那个字符
*/
public static void main(String[] args) {
String s = "hasjdlkfjlasjd;fj";
HashMap<Character,Integer> hs= new HashMap<Character,Integer>();
//依次取出每个字符,存在一个HashMap中,如果已经存在存过,次数+1
for(int i=0;i<s.length();i++) {
Character ch = Character.valueOf(s.charAt(i));
Integer num = hs.get(ch);
hs.put(ch,num == null ? 1 :num.intValue() + 1);
}

//System.out.println(hs);
//遍历HashMap,找出出现次数最多的字符
Iterator it = hs.keySet().iterator();
Character maxChar;
int maxNum = 0;
int tempNum;
Character tempchar = new Character(' ');
while(it.hasNext()) {
tempchar = (Character)it.next();
tempNum = hs.get(tempchar);
if(tempNum > maxNum) {
maxNum = tempNum;
maxChar = tempchar;

}
}

System.out.println(tempchar+ " " + maxNum);
}

}
John_2001_83 2007-12-27
  • 打赏
  • 举报
回复
用for循环找一下就好了。
public int findS(String str){
int count=0;
for(int i=0;i<str.length;i++)
if(str.charAt(i)=='S')count++;
return count;
}
海会圣贤 2007-12-27
  • 打赏
  • 举报
回复
把字符串S付给一个char数组,用一个for,和一个if就可以搞定。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧