public class Anagram {
public static void main(String[] args) {
String a = "Despera tion-".toLowerCase();
String b = "A Rope Ends It".toLowerCase();
String aSorted = sortStringAlphabetically(a);
String bSorted = sortStringAlphabetically(b);
if(aSorted.equals(bSorted)){
System.out.println("Anagram Found!");
}else{
System.out.println("No anagram was found");
}
}
public static String sortStringAlphabetically(String s) {
char[] ca = s.toCharArray();
int cnt = 0;
ArrayList al = new ArrayList();
for (int i = 0; i < ca.length; i++) {
if (Character.isLetter(ca[cnt]))
al.add(ca[cnt]);
cnt++;
}
Collections.sort(al);
return al.toString();
}
}
作为一名学习者,我打上了这台蓝色分析器。 我所选择的解决办法是,采用一种分类法似乎太过了分类法——和;果园。 -> ArrayList ->String——鉴于我确实想比较两条插图,以检验一个短语是否是另一个短语的图象——我是否能够用较少的分类来做?
ps The tutors solution was a mile away from my attempt, and probably much better for a lot of reasons - but I am really trying to get a handle on all the different Collection types.
EDIT
在这里,公平与和解委员会是最初的挑战,我相信,我放弃了解决办法。
http://www.home.hs-karlsruhe.de/~pach0003/informatik_1/aufgaben/en/arrays.html
我最初的kneejerk的反应是,仅仅通过阵列开展工作,打碎与阵列相匹配的果园,但这似乎要求我重建每一处的阵列——许多人感谢你们为教育我所做的一切努力。