What is an easy way to tell if a list of words are anagrams of each other?

匆匆过客 提交于 2019-11-27 15:17:20

Put all the letters in alphabetical order in the string (sorting algorithm) and then compare the resulting string.

-Adam

Good thing we all live in the C# reality of in-place sorting of short words on quad core machines with oozles of memory. :-)

However, if you happen to be memory constrained and can't touch the original data and you know that those words contain characters from the lower half of the ASCII table, you could go for a different algorithm that counts the occurrence of each letter in each word instead of sorting.

You could also opt for that algorithm if you want to do it in O(N) and don't care about the memory usage (a counter for each Unicode char can be quite expensive).

Sort each element (removing whitespace) and compare against the previous. If they are all the same, they're all anagrams.

Interestingly enough, Eric Lippert's Fabulous Adventures In Coding Blog dealt with a variation on this very problem on February 4, 2009 in this post.

The following algorithm should work:

  1. Sort the letters in each word.

  2. Sort the sorted lists of letters in each list.

  3. Compare each element in each list for equality.

Well Sort the words in the list.

if abc, bca, cab, cba are the inputs, then the sorted list will be abc, abc, abc, abc.

Now all of their Hash codes are equal. Compare the HashCodes.

Sort the letters and compare (letter by letter, string compare, ...) is the first things that comes to mind.

  1. compare length (if not equal, not a chance)
  2. make a bit vector of the length of the strings
  3. for each char in the first string find occurrences of it in the second
  4. set the bit for the first unset occurrence
  5. if you can find one stop with fail
public static void main(String[] args) {

    String s= "abc";
    String s1="cba";



     char[] aArr = s.toLowerCase().toCharArray(); 
     char[] bArr = s1.toLowerCase().toCharArray();

  // An array to hold the number of occurrences of each character
  int[] counts = new int[26]; 

  for (int i = 0; i < aArr.length; i++){
   counts[aArr[i]-97]++;  // Increment the count of the character at respective position
   counts[bArr[i]-97]--;  // Decrement the count of the character at respective position
  }

  // If the strings are anagrams, then counts array will be full of zeros not otherwise
  for (int i = 0; i<26; i++){
   if (counts[i] != 0)
    return false;
  } 

Tried hashcode logic for anagram gives me false output

public static Boolean anagramLogic(String s,String s2){
    char[] ch1 = s.toLowerCase().toCharArray();
        Arrays.sort(ch1);
        char[] ch2= s2.toLowerCase().toCharArray();
        Arrays.sort(ch2);
        return ch1.toString().hashCode()==ch2.toString().hashCode(); //wrong
    }

to rectify this code, below is the only option I see,appreciate any recommendations

char[] ch1 = s.toLowerCase().toCharArray();
        Arrays.sort(ch1);
        char[] ch2= s2.toLowerCase().toCharArray();
        Arrays.sort(ch2);
        return Arrays.equals(ch1,ch2);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!