A client-side router caches pages by a hash of the route path. The hash of a string s of length n is code(s[0]) × 31^0 + code(s[1]) × 31^1 + ... + code(s[n-1]) × 31^(n-1), where code is charCodeAt.
Write routeHash(path) that returns this hash modulo 1000000007. The hash of the empty string is 0.
Input: path = "ab" Output: 3135
97 × 1 + 98 × 31 = 97 + 3038 = 3135.
Input: path = "a" Output: 97
One character: its code times 31^0 = 1.
0 <= path.length <= 10^5path contains printable ASCII characters.Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #644 Route Path Hash. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.