返回介绍

solution / 2100-2199 / 2156.Find Substring With Given Hash Value / README_EN

发布于 2024-06-17 01:03:09 字数 4133 浏览 0 评论 0 收藏 0

2156. Find Substring With Given Hash Value

中文文档

Description

The hash of a 0-indexed string s of length k, given integers p and m, is computed using the following function:

  • hash(s, p, m) = (val(s[0]) * p0 + val(s[1]) * p1 + ... + val(s[k-1]) * pk-1) mod m.

Where val(s[i]) represents the index of s[i] in the alphabet from val('a') = 1 to val('z') = 26.

You are given a string s and the integers power, modulo, k, and hashValue. Return sub,_ the first substring of _s_ of length _k_ such that _hash(sub, power, modulo) == hashValue.

The test cases will be generated such that an answer always exists.

A substring is a contiguous non-empty sequence of characters within a string.

 

Example 1:

Input: s = "leetcode", power = 7, modulo = 20, k = 2, hashValue = 0
Output: "ee"
Explanation: The hash of "ee" can be computed to be hash("ee", 7, 20) = (5 * 1 + 5 * 7) mod 20 = 40 mod 20 = 0. 
"ee" is the first substring of length 2 with hashValue 0. Hence, we return "ee".

Example 2:

Input: s = "fbxzaad", power = 31, modulo = 100, k = 3, hashValue = 32
Output: "fbx"
Explanation: The hash of "fbx" can be computed to be hash("fbx", 31, 100) = (6 * 1 + 2 * 31 + 24 * 312) mod 100 = 23132 mod 100 = 32. 
The hash of "bxz" can be computed to be hash("bxz", 31, 100) = (2 * 1 + 24 * 31 + 26 * 312) mod 100 = 25732 mod 100 = 32. 
"fbx" is the first substring of length 3 with hashValue 32. Hence, we return "fbx".
Note that "bxz" also has a hash of 32 but it appears later than "fbx".

 

Constraints:

  • 1 <= k <= s.length <= 2 * 104
  • 1 <= power, modulo <= 109
  • 0 <= hashValue < modulo
  • s consists of lowercase English letters only.
  • The test cases are generated such that an answer always exists.

Solutions

Solution 1

/**
 * @param {string} s
 * @param {number} power
 * @param {number} modulo
 * @param {number} k
 * @param {number} hashValue
 * @return {string}
 */
var subStrHash = function (s, power, modulo, k, hashValue) {
  power = BigInt(power);
  modulo = BigInt(modulo);
  hashValue = BigInt(hashValue);
  const n = s.length;
  let pk = 1n;
  let ac = 0n;
  // 倒序滑动窗口
  for (let i = n - 1; i > n - 1 - k; i--) {
    ac = (ac * power + getCode(s, i)) % modulo;
    pk = (pk * power) % modulo;
  }
  let ans = -1;
  if (ac == hashValue) {
    ans = n - k;
  }
  for (let i = n - 1 - k; i >= 0; i--) {
    let pre = (getCode(s, i + k) * pk) % modulo;
    ac = (ac * power + getCode(s, i) - pre + modulo) % modulo;
    if (ac == hashValue) {
      ans = i;
    }
  }
  return ans == -1 ? '' : s.substring(ans, ans + k);
};

function getCode(str, index) {
  return BigInt(str.charCodeAt(index) - 'a'.charCodeAt(0) + 1);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文