Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique hash for same set of characters given in any order?

Consider this example of finding anagrams

aabc
abca

They both are anagrams, I am looking for a way so that their hash generated by characters is same and unique.

The uniqueness is important so that no two different string aabc and xyaq generate the same hash

I have no idea about this, but throwing up here to learn what I need to look up for

like image 954
daydreamer Avatar asked Oct 22 '25 04:10

daydreamer


1 Answers

Sort the characters in the string and use that as the hash. Strings will have the same hash if they are anagrams of each other:

String anagramHash(String str) {
    char[] chars = str.toCharArray();
    Arrays.sort(chars);
    return new String(chars);
}

This probably won't work if you have code points which aren't on the BMP (http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html).

Alternatively, generate a histogram and use that as the hash.

like image 100
Adrian Leonhard Avatar answered Oct 25 '25 01:10

Adrian Leonhard