Doubt on the code of string hashing

long long compute_hash(string const& s) {
const int p = 31;
const int m = 1e9 + 9;
long long hash_value = 0;
long long p_pow = 1;
for (char c : s) {
hash_value = (hash_value + (c - β€˜a’ + 1) * p_pow) % m;
p_pow = (p_pow * p) % m;
}
return hash_value;
}
This is the code for the string hashing ,What is the significance of this expression β€œ(c - β€˜a’ + 1) '”??

hello @Somasree

it converts character to int .

for example
if character c is β€˜a’ then it will give 1
if character c is β€˜b’ then it will give 2
if character c is β€˜c’ then it will give 3
if character c is β€˜d’ then it will give 4
so on…

ok thank u so much :grinning:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Please explain to me how the ans is 45.
Is there a formula n*(n-1)/2. used here

yeah . . …
if we have n vertices, then we can pick two vertices and draw an edge between then. that why nC2 = n *(n-1)/2

ok understood thank u :smiley: