2014-09-06 73 views
-1

你好家伙我有我的代码有问题,我想获得字符串中最频繁的字母。这是我到目前为止已经试过:计算字符串中最频繁的字母Java

String s = "Sashimi"; 
    int highestFreq = 0; 
    char mostFreqChar = ' '; 
    for (int i = 0; i < s.length(); i++) 
     { 

      char x = s.charAt(i); 
      int c = 0; 
      for (int j = s.indexOf(x); j != -1; j = s.indexOf(x, j + 1)) 
      { 
       c++; 
      } 
      if (c > highestFreq) 
      { 
       highestFreq = c; 
       mostFreqChar = x; 
      } 
     } 
     System.out.println("Most frequent character in " + s + " is " + mostFreqChar); 
    } 

然而,它只是显示字母“I”,但在我的例子上面的字符串是生鱼片。所以有两个最常见的字母,即S和I.我的代码有什么问题?任何帮助将不胜感激。谢谢。

+0

考虑一个'地图<字符,整数>' – user2864740 2014-09-06 03:03:39

+0

@ user2864740你可以做的一些样品?我是一个新手谢谢。 – 2014-09-06 03:05:20

+0

请参阅http://stackoverflow.com/questions/4327226/highest-frequency-of-letters-in-each-line-of-file-core-java-programming?rq=1等 - 使用“重复“问题是一个相似的原则;首先制作计数桶(例如字符 - >计数),然后查找计数最高的密钥。 – user2864740 2014-09-06 03:07:05

回答

1

您只允许设置char mostFreqChar,而不是增强。因此,大多数弗雷卡尔只能是一个项目。此外,如果您希望它显示最频繁的多个字符,则应使用字符串,char数组或char的向量。 您需要更改您的代码,以便您不仅拥有: mostFreqChar = x; ,因为如果你只有上面的行,mostFreqChar只能等于分配给它的最后一个字符X.

如果您需要更多说明,请在下面评论,我会尽力回复。

我会使用类似于C++的东西。注意:我没有调试过,并且在C++中字符串被视为字符数组。

#include <string> 
using namespace std; 

bool contains_(string str, char Contained_) 
{ 
    for(int i = 0; i < str.size(); i++) 
    { 
     if (str[i] == Contained_) return true; 
    } 
    return false; 
} 


int main() 
{ 
    string s = "Sasami"; 
    string Used_characters = ""; 
    int Frequency_list[s.size()]; 

    for (int i = 0; i < s.size(); i++) //initializes the aray 
    { 
     Frequency_list[i] = 0; 
    } 

    for(int i = 0; i < s.size(); i++) 
    { 
     if (contains_(Used_characters, s[i])) //makes sure the program 
     //doesn't return 2 of the same character 
     { 
      for(int a = 0; a < s.size(); a++) 
      { 
       if (s[i] == s[a]) Frequency_list[i] ++; 
      } 
     } 
    } 

    //Now find which has the highest frequency, and match it to the string 

    char Max_char[s.size()]; 
    for (int i = 0; i < s.size(); i++) //initializes the aray 
    { 
     Max_char[i] = '0'; //I choose 0 because it is not a letter 
    } 

    int Max_freq = 0; 
    int num_of_max_freq = 0; 
    for(int i = 0; i < s.size(); i++) 
    { 
     if (Frequency_list[i] > Max_freq) 
     { 
      Max_freq = Frequency_list[i]; 
      for (int i = 0; i < s.size(); i++) //Whipes the array clear 
      { 
       Max_char[i] = '0'; //I choose 0 because it is not a letter 
      } 
      num_of_max_freq = 1; 
      Max_char[0] = s[i]; 
     } 
     else if (Frequency_list[i] == Max_freq) 
     { 
      Max_char[num_of_max_freq++] = s[i]; 
     } 
    } 
    //Now all your max_values are stored in the array Max_char 

} 
+0

你能提供一些样例实现吗?我有点困惑,谢谢。 – 2014-09-06 03:03:52

+0

当然,给我几分钟来做一个快速的代码。请注意我通常使用C++,所以我没有Java编辑器。代码仍然可以工作。 – 2014-09-06 03:07:19

1

的问题是,在规划,资金S和小写s是两个不同的字符,所以显示i应该是预期的结果。但是,如果你想列举几个字符,你可以这样做:

String s = "Sashimi"; 
int highestFreq = 0; 
List<Character> mostFrequentChars = ArrayList<>(); 
for (int i = 0; i < s.length(); i++) 
    { 

     char x = s.charAt(i); 
     int c = 0; 
     for (int j = s.indexOf(x); j != -1; j = s.indexOf(x, j + 1)) 
     { 
      c++; 
     } 
     if (c > highestFreq) 
     { 
      mostFrequentChars.removeAll(); 
      highestFreq = c; 
      mostFrequentChars.add(new Character(x)); 
     }else if (c == highestFreq){ 
      mostFrequentChars.add(new Character(x)); 
     } 
    } 
    System.out.print("Most frequent characters in " + s + " are "); 
    for (Character c:mostFrequentChars){ 
     System.out.print(c); 
    } 
    System.out.println(); 

}

+0

我添加了一些代码,以便它返回多个字符 – DrOverbuild 2014-09-06 03:08:35

+0

只是一个疯狂的猜测,但我认为代码返回/打印两个'i'字符。如果真的发生了,你可以尝试使用'Set'而不是'List'。 – Tom 2014-09-06 03:11:19