Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

As vnp pointed out in this answer this answer, your original algorithm only considers if both strings have the same letters, not checking if those letters occur the same number of times. Is there a better way to check for an anagram?

Note that this algorithm is still incomplete for detecting anagrams, as detailed by this comment this comment. Additional improvements can and probably should be added.

As vnp pointed out in this answer, your original algorithm only considers if both strings have the same letters, not checking if those letters occur the same number of times. Is there a better way to check for an anagram?

Note that this algorithm is still incomplete for detecting anagrams, as detailed by this comment. Additional improvements can and probably should be added.

As vnp pointed out in this answer, your original algorithm only considers if both strings have the same letters, not checking if those letters occur the same number of times. Is there a better way to check for an anagram?

Note that this algorithm is still incomplete for detecting anagrams, as detailed by this comment. Additional improvements can and probably should be added.

added 314 characters in body
Source Link
nickb
  • 221
  • 2
  • 7

Note that this algorithm is still incomplete for detecting anagrams, as detailed by this comment . Additional improvements can and probably should be added.

Note that this algorithm is still incomplete for detecting anagrams, as detailed by this comment . Additional improvements can and probably should be added.

added 589 characters in body
Source Link
nickb
  • 221
  • 2
  • 7

As vnp pointed out in this answer, your original algorithm only considers if both strings have the same letters, not checking if those letters occur the same number of times. Is there a better way to check for an anagram?

What if we took each string, split it into an array of all of its letters, and sorted those arrays? We would have an anagram if those arrays were identical. You added a good implementation to your post, but it still needs improvement:

char [] c1 = new char [str1.length()];
char [] c2 = new char [str2.length()];
Arrays.sort(c1);
Arrays.sort(c2);

I'll leave this implementation upYou create c1 and c2 but never store anything in them! So, your algorithm will always return true when passed two strings of equal length, because it's really only comparing empty arrays.

Java already gives us String.toCharArray() which will take any String and produce a char[] from it, so use it, and you don't have to worry about manually creating the OParrays:

char[] c1 = str1.toCharArray();
char[] c2 = str2.toCharArray();

Also, if you just return the value you're getting from Arrays.equals(), you won't have to keep the isAnagram variable around anymore.

public class Anagram {
 public boolean isAnagram(String str1, String str2) {
 if(str1.length() != str2.length()) {
 return false;
 }
 booleanchar[] isAnagramc1 = false;str1.toCharArray();
 //char[] Fixc2 algorithm,= setstr2.toCharArray();
 isAnagram accordingly Arrays.sort(c1);
 Arrays.sort(c2);

 return isAnagram;Arrays.equals(c1, c2);
 }
 public static void main(String[] args) {
 Anagram anagram = new Anagram();
 System.out.println(anagram.isAnagram("mary","army"));
 }
}

As vnp pointed out in this answer, your algorithm only considers if both strings have the same letters, not checking if those letters occur the same number of times. Is there a better way to check for an anagram?

What if we took each string, split it into an array of all of its letters, and sorted those arrays? We would have an anagram if those arrays were identical.

I'll leave this implementation up to the OP.

public class Anagram {
 public boolean isAnagram(String str1, String str2) {
 if(str1.length() != str2.length()) {
 return false;
 }
 boolean isAnagram = false;
 // Fix algorithm, set isAnagram accordingly
 return isAnagram;
 }
 public static void main(String[] args) {
 Anagram anagram = new Anagram();
 System.out.println(anagram.isAnagram("mary","army"));
 }
}

As vnp pointed out in this answer, your original algorithm only considers if both strings have the same letters, not checking if those letters occur the same number of times. Is there a better way to check for an anagram?

What if we took each string, split it into an array of all of its letters, and sorted those arrays? We would have an anagram if those arrays were identical. You added a good implementation to your post, but it still needs improvement:

char [] c1 = new char [str1.length()];
char [] c2 = new char [str2.length()];
Arrays.sort(c1);
Arrays.sort(c2);

You create c1 and c2 but never store anything in them! So, your algorithm will always return true when passed two strings of equal length, because it's really only comparing empty arrays.

Java already gives us String.toCharArray() which will take any String and produce a char[] from it, so use it, and you don't have to worry about manually creating the arrays:

char[] c1 = str1.toCharArray();
char[] c2 = str2.toCharArray();

Also, if you just return the value you're getting from Arrays.equals(), you won't have to keep the isAnagram variable around anymore.

public class Anagram {
 public boolean isAnagram(String str1, String str2) {
 if(str1.length() != str2.length()) {
 return false;
 }
 char[] c1 = str1.toCharArray();
 char[] c2 = str2.toCharArray();
  Arrays.sort(c1);
 Arrays.sort(c2);

 return Arrays.equals(c1, c2);
 }
 public static void main(String[] args) {
 Anagram anagram = new Anagram();
 System.out.println(anagram.isAnagram("mary","army"));
 }
}
Source Link
nickb
  • 221
  • 2
  • 7
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /