1
''' class Solution { public : string subStrHash(string s, int power, int modulo,2
int k, int hashValue) {6
// Intializing a window from end of size k7
for (int i = 0; i < k; i++) {8
sum = (sum + ((s[n - k + i] - 'a' + 1) * p));9
if (i != k - 1) p = (p * power) % modulo;11
// storing the res when ever we get required answer12
int res = n - k; // storing the res when ever we get required answer13
// slide window to right and removing ith index element and14
// adding (i-k)th index element15
for (int i = n - 1; i >= k; i--) {16
// removing last element from window17
sum -= (s[i] - 'a' + 1) * (p % modulo);18
// dividing by modulo to avoid integer overflow conditions20
// muliplying the given string by power21
sum = sum * (power % modulo);22
// adding (i-k)th element in the window23
sum += s[i - k] - 'a' + 1;24
// if sum < 0 then it created problem in modulus thats why making it26
while (sum % modulo < 0) {29
if (sum % modulo == hashValue) {30
res = i - k; // storing the starting window index because we have to31
// return the first string from starting34
return s.substr(res, k);