I am looking for an efficient solution to check if two strings are anagrams, but char table/dictionary checking may not be a good solution for unicode. I have come up a solution, but I don’t know how to prove it mathematically correct. The formula is in the express as ” (a + b) = (c + d) and a XOR b XOR c XOR d = 0 ==> (a,b) and (c,d) are anagrams”. Maybe you can help me. The following is an implementation.
def isAnagram(s1: String, s2: String): Boolean = {
if (s1.length != s2.length) return false
else {
var numVal = 0
var bitVal = 0
for (i <- 0 until s1.length) {
numVal += s1(i) - s2(i)
bitVal ^= s1(i) ^ s2(i)
}
return numVal == 0 && bitVal == 0
}
icepack’s counter example is incorrect, as
1 ^ 5 ^ 3 ^ 2 ^ 4 ^ 3 = 2, not0.Here’s a better counter example:
s1 = (5, 0, 5)ands2 = (1, 4, 5).5 + 0 + 5 = 1 + 4 + 55 ^ 0 ^ 5 ^ 1 ^ 4 ^ 5 = 0