Skip to main content
Code Review

Return to Answer

deleted 3 characters in body
Source Link

One line - one statement

This will make the code more readable.

temp = str2.toCharArray();
Arrays.sort(temp);
str2 = new String(temp);
 
if(str1.equals(str2) == true) 
 System.out.println("Anagrams");
else 
 System.out.println("Not Anagrams");

Separate logic and input/output

This look much cleaner:

public static void main(String[] args) {
 Scanner input = new Scanner(System.in);
 String str1 = input.nextLine().toLowerCase();
 String str2 = input.nextLine().toLowerCase();
 if(isAnagramsisAnagram(str1, str2))
 System.out.println("Anagrams");
 else 
 System.out.println("Not Anagrams");
}
bool isAnagramsisAnagram(String s1, String s2) {...}

All the logic is in isAnagramsisAnagram function; it doesn't mix with input/output and can potentionally be reused in other projects.

Be careful with Unicode

If you are using non-ASCII characters, the effects of toLowerCase/toUpperCase and breaking into characters can be unexpected, like "ß".toUpperCase() producing "SS" or "á" being two characters. I'll assume you're using ASCII character set.

Use Arrays.equals

Arrays.equals method can compare arrays, so you can avoid gathering arrays back in strings.

Algorithm

To compare to strings for being anagrams, you need to find every symbol of one string in another (naive algorithm will give \$O(n^2)\$), and sorting makes it faster (\$O(n log(n))\$). Could it be even faster? Maybe - if you choose the proper sorting algorithm. If you're using ASCII characters only and words are long enough (tens of characters), counting sort will allow you to exchange some memory for speed. You don't even need to recreate a sorted sequence - just:

  1. compare lengths
  2. add the number of characters in the first string
  3. subtract the number of characters in the second string, if you ever get a negative value - the strings are not anagrams.

One line - one statement

This will make the code more readable.

temp = str2.toCharArray();
Arrays.sort(temp);
str2 = new String(temp);
 
if(str1.equals(str2) == true) 
 System.out.println("Anagrams");
else 
 System.out.println("Not Anagrams");

Separate logic and input/output

This look much cleaner:

public static void main(String[] args) {
 Scanner input = new Scanner(System.in);
 String str1 = input.nextLine().toLowerCase();
 String str2 = input.nextLine().toLowerCase();
 if(isAnagrams(str1, str2))
 System.out.println("Anagrams");
 else 
 System.out.println("Not Anagrams");
}
bool isAnagrams(String s1, String s2) {...}

All the logic is in isAnagrams function; it doesn't mix with input/output and can potentionally be reused in other projects.

Be careful with Unicode

If you are using non-ASCII characters, the effects of toLowerCase/toUpperCase and breaking into characters can be unexpected, like "ß".toUpperCase() producing "SS" or "á" being two characters. I'll assume you're using ASCII character set.

Use Arrays.equals

Arrays.equals method can compare arrays, so you can avoid gathering arrays back in strings.

Algorithm

To compare to strings for being anagrams, you need to find every symbol of one string in another (naive algorithm will give \$O(n^2)\$), and sorting makes it faster (\$O(n log(n))\$). Could it be even faster? Maybe - if you choose the proper sorting algorithm. If you're using ASCII characters only and words are long enough (tens of characters), counting sort will allow you to exchange some memory for speed. You don't even need to recreate a sorted sequence - just:

  1. compare lengths
  2. add the number of characters in the first string
  3. subtract the number of characters in the second string, if you ever get a negative value - the strings are not anagrams.

One line - one statement

This will make the code more readable.

temp = str2.toCharArray();
Arrays.sort(temp);
str2 = new String(temp);
 
if(str1.equals(str2) == true) 
 System.out.println("Anagrams");
else 
 System.out.println("Not Anagrams");

Separate logic and input/output

This look much cleaner:

public static void main(String[] args) {
 Scanner input = new Scanner(System.in);
 String str1 = input.nextLine().toLowerCase();
 String str2 = input.nextLine().toLowerCase();
 if(isAnagram(str1, str2))
 System.out.println("Anagrams");
 else 
 System.out.println("Not Anagrams");
}
bool isAnagram(String s1, String s2) {...}

All the logic is in isAnagram function; it doesn't mix with input/output and can potentionally be reused in other projects.

Be careful with Unicode

If you are using non-ASCII characters, the effects of toLowerCase/toUpperCase and breaking into characters can be unexpected, like "ß".toUpperCase() producing "SS" or "á" being two characters. I'll assume you're using ASCII character set.

Use Arrays.equals

Arrays.equals method can compare arrays, so you can avoid gathering arrays back in strings.

Algorithm

To compare to strings for being anagrams, you need to find every symbol of one string in another (naive algorithm will give \$O(n^2)\$), and sorting makes it faster (\$O(n log(n))\$). Could it be even faster? Maybe - if you choose the proper sorting algorithm. If you're using ASCII characters only and words are long enough (tens of characters), counting sort will allow you to exchange some memory for speed. You don't even need to recreate a sorted sequence - just:

  1. compare lengths
  2. add the number of characters in the first string
  3. subtract the number of characters in the second string, if you ever get a negative value - the strings are not anagrams.
Source Link

One line - one statement

This will make the code more readable.

temp = str2.toCharArray();
Arrays.sort(temp);
str2 = new String(temp);
 
if(str1.equals(str2) == true) 
 System.out.println("Anagrams");
else 
 System.out.println("Not Anagrams");

Separate logic and input/output

This look much cleaner:

public static void main(String[] args) {
 Scanner input = new Scanner(System.in);
 String str1 = input.nextLine().toLowerCase();
 String str2 = input.nextLine().toLowerCase();
 if(isAnagrams(str1, str2))
 System.out.println("Anagrams");
 else 
 System.out.println("Not Anagrams");
}
bool isAnagrams(String s1, String s2) {...}

All the logic is in isAnagrams function; it doesn't mix with input/output and can potentionally be reused in other projects.

Be careful with Unicode

If you are using non-ASCII characters, the effects of toLowerCase/toUpperCase and breaking into characters can be unexpected, like "ß".toUpperCase() producing "SS" or "á" being two characters. I'll assume you're using ASCII character set.

Use Arrays.equals

Arrays.equals method can compare arrays, so you can avoid gathering arrays back in strings.

Algorithm

To compare to strings for being anagrams, you need to find every symbol of one string in another (naive algorithm will give \$O(n^2)\$), and sorting makes it faster (\$O(n log(n))\$). Could it be even faster? Maybe - if you choose the proper sorting algorithm. If you're using ASCII characters only and words are long enough (tens of characters), counting sort will allow you to exchange some memory for speed. You don't even need to recreate a sorted sequence - just:

  1. compare lengths
  2. add the number of characters in the first string
  3. subtract the number of characters in the second string, if you ever get a negative value - the strings are not anagrams.
lang-java

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