2014-09-06 72 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