Skip to the code
cf.
+25 XP

#644 Route Path Hash

medium

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.

Example 1

Input: path = "ab"
Output: 3135

97 × 1 + 98 × 31 = 97 + 3038 = 3135.

Example 2

Input: path = "a"
Output: 97

One character: its code times 31^0 = 1.

Constraints

  • 0 <= path.length <= 10^5
  • path contains printable ASCII characters.
Saved in this browser

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).