public class Solution {
private static int[] primeNumbers =
new [] {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101};
public bool IsAnagram(string s, string t) {
return (GetChecksum(s) == GetChecksum(t));
}
public static int GetChecksum(string s)
{
int checksum = 0;
foreach(char c in s)
{
var digit = c.GetHashCode() % 26;
checksum += primeNumbers[digit] * (digit + 1);
}
return checksum;
}
}
C# solution using prime numbers


@EntityManager said in C# solution using prime numbers:
Please tell me , why have you used the method hashcode?
Can you just not take the integer value for the character and do a modulo 26?
Also why have you used digit+1, when you calculate the checksum?Hashing is O(1). I don't really care how it does it, as long as it gives me a unique int. In fact, it could well be possible that char hashing is just using its Unicode int value. However that's not the concern of this algorithm.
The variable digit can be zero. Anything multiplies 0 is 0 so the context can be lost. It is added by 1 to prevent that.


hashcode is not required, you can use "charCodeAt()  97" or "charCodeAt()  65" for uppercase

I understand prime factorization, so if
checksum *= primeNumbers[digit]
checksum will be unique
But for this line of code, what is the math theorem that supports checksum to be unique?
checksum += primeNumbers[digit] * (digit + 1)
