In one of the Coursera videos the Rabin-Karp rolling hash (http://en.wikipedia.org/wiki/Rolling_hash) is shown as:
public static long getHash(String S)
{
long H = 0;
for (int i = 0; i < S.length(); i++)
H = (H * 10 + S.charAt(i)) % 997;
return H;
}
I think it's wrong. I think it should be :
public static long getHash(String S)
{
long H = 0;
for (int i = 0; i < S.length(); i++)
H = (S.charAt(i) * (int)Math.pow(10, (S.length() - i - 1)) + H) % 997;
return H;
}
Which one is correct and why?