2012-04-05 64 views
0

我應該寫一個算法,從給定的單詞集中挑選出anagrams。到目前爲止,我得到了這個Sorting Anagrams

​​

任何人都可以請幫我嗎?我正在努力與分揀部分。我不知道如何使用我得到的這段代碼,並將其轉換爲字符串,並將其分類爲字符。

+2

你的輸入包含什麼內容?每行只有一個字或已經有一些字符,你只需要排序。或者你是否需要從輸入的單詞中構建字符?你必須更精確地得到建設性的答案。 – s1lence 2012-04-05 11:52:49

回答

0
Vector<String> strings = new Vector<String>(); 
while ((SLine = br.readLine()) != null) //read the txt.file line by line 
{ 
    strings.add(SLine); 
    System.out.println(SLine); //print out the words 
} 

現在,您可以使用Vector中的字符串對它們進行排序。 開創全省的排序算法作爲單獨的功能:

void sortStrings(Vector<String> strings) { 
// ... 
} 

如何做好acutal排序,你可以找到你自己,還有avalible很多東西:What function can be used to sort a Vector?

0

找出如果兩個詞是字謎不如果你這樣想的話,真的很複雜:只要兩個字符串的長度和字母相同,它們就是字符。

因此,您需要編寫一個方法來接收兩個字符串,對這些字符串進行排序並檢查它們是否在同一個索引上具有相同的值。在代碼中這將是這個東西simular:

public boolean isAnagram(String str1, String str2) { 
    if (str1.length() != str2.length()) 
     return false; // can't be an anagram since not equal length 
    for (int i = i<str1.length;i++) { // loop thru the string 
     if (str1.charAt(i) != str2.charAt(i)) // is the char at index i in str1 not equal to char at the same index in str2? 
      return false; 
     } 
     return true; 

請注意,對於這種方法的工作,琴絃需要進行排序,不應該包含空格等。由於這是功課我會留給工作爲你做:)

0

如何使所有這些字符串進入數組列表比排序數組列表。 當你打印添加到數組列表然後對其進行排序..簡單編碼..