2013-10-24 42 views
-1

我有一個包含重複字的字符串,我需要計算同時發生的字符串中唯一的重複字。計數同時發生在字符串中的字

String a="am abc am am am xyz as xyz xyz xyz xyz"; 

上面的字符串我需要計數am其中occures同時即上午occures 3時間XYZ occures 4時間並在此我需要僅顯示XYZ = 4這是最大同時重複。任何人都可以幫助我。

+4

好了,你有什麼嘗試? –

+0

「同時」可能是錯誤的詞(我看你不是英語母語的人,這很好)。也許你的意思是「連續」?另外,像這樣的SO問題確實需要顯示你已經嘗試了什麼,以及你有什麼確切的問題。 SO不是「爲我編碼」或「給我如何做到這一點的一般提示」網站。 – hyde

回答

3
1. Create a HashMap of Strings and Integer. 
2. Split the given String with the delimiter and create an array of Strings[]. 
3. Loop through the array of Strings[] and check if the String exists in HashMap, 
3.1 if yes, increment the value by 1. 

關鍵詞這裏:

  1. HashMap
  2. Split the Strings

希望這有助於。

+0

我認爲它會統計所有重複,我只需要同時發生重複字數。 – Basavaraj

+0

我只是想給你一個方向。你將不得不實施這個邏輯。 :)嘗試執行邏輯,如果卡住了,可以重新提出問題。 – JNL