Ordering a string in alphabetical order - did I miss something obvious?

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(); } } 

As a student, I cracked this logical Anagram test. My chosen solution was to create a sortStringAlphabetically method, there seemed to be too much juggling type String -> chars [] -> ArrayList -> String - given that I just want to compare 2 strings to check if one phrase is anagram another - could I do is it with less juggling type?

ps The solution for teachers was a mile from my attempt and probably a lot better for many reasons, but I'm really trying to understand all types of collections.

http://www.home.hs-karlsruhe.de/~pach0003/informatik_1/aufgaben/en/doc/src-html/de/hska/java/exercises/arrays/Anagram.html#line.18

EDIT

FTW is an original task, I understand that I have left the solution.

http://www.home.hs-karlsruhe.de/~pach0003/informatik_1/aufgaben/en/arrays.html

My initial reaction to the knee was to just work with array a, picking those characters that matched array b - but that would seem to require me to rebuild the array at each iteration. Thanks so much for all your efforts in teaching me.

+4
source share
4 answers

There are various ways to improve this if you go with this algorithm. First, you do not have to create an array of characters. You can use String.charAt () to access the specific character of your string.

Secondly, you do not need a list. If you used SortedMultiSet or SortedBag, you can just add things in sorted order. If you write a function that creates a SortedMultiSet from your string, you can simply compare the sets without rebuilding the string.

Note. I don’t know which libraries you are allowed to use (Google and Apache have these types), but you can always “brew your own”.

Also, be sure to use generic types for your types. Just defining ArrayLists is pretty risky, IMHO.

+3
source

Your algorithm, but shorter (and nevertheless, slower). Juggling type is performed “implicitly” in Java by various library classes:

 public static boolean isAnagram(String a, String b) { List<String> listA = new ArrayList<String>(Arrays.asList( a.toLowerCase().replaceAll("\\W", "").split(""))); List<String> listB = new ArrayList<String>(Arrays.asList( b.toLowerCase().replaceAll("\\W", "").split(""))); Collections.sort(listA); Collections.sort(listB); return listA.equals(listB); } 

Optionally replace the regular expression \W to exclude letters that you do not want to be considered for anagram

+1
source

You can simply sort the row without using a list:

 public static String sortStringAlphabetically(String s) { String lettersOnly = s.replaceAll("\\W", ""); char[] chars = lettersOnly.toCharArray(); Arrays.sort(chars); return new String(chars); } 

NB I really did not try to use the code.

+1
source
 public class Anagram { public static void main(String[] args) throws Exception { String s1 = "Despera tion-"; String s2 = "A Rope Ends It"; anagramCheck(s1, s2); } private static void anagramCheck(String s1, String s2) { if (isAnagram(s1, s2)) { System.out.println("Anagram Found!"); } else { System.out.println("No anagram was found"); } } private static boolean isAnagram(String s1, String s2) { return sort(s1).equals(sort(s2)); } private static String sort(String s) { char[] array = s.replaceAll("\\W", "").toLowerCase().toCharArray(); Arrays.sort(array); return new String(array); } } 
+1
source

Source: https://habr.com/ru/post/1382294/


All Articles