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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With