Solving Leetcode Interviews in Seconds with AI: Find Substring With Given Hash Value
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2156" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
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.
Explanation
- Rolling Hash: The solution uses the rolling hash technique to efficiently compute the hash value of each substring of length
k. This avoids recomputing the entire hash for each substring, resulting in significant time savings.- Iterate and Compare: The code iterates through the string
s, computing the hash value of each substring of lengthkusing the rolling hash. It compares the computed hash value with the targethashValue. - Return First Match: The first substring whose hash value matches
hashValueis returned.
- Iterate and Compare: The code iterates through the string
- Runtime Complexity: O(n), where n is the length of the string
s. - Storage Complexity: O(1)
Code
def subStrHash(s: str, power: int, modulo: int, k: int, hashValue: int) -> str:
"""
Finds the first substring of length k with the given hash value.
Args:
s: The input string.
power: The power to use for the hash function.
modulo: The modulo to use for the hash function.
k: The length of the substring.
hashValue: The target hash value.
Returns:
The first substring of length k with the given hash value.
"""
n = len(s)
current_hash = 0
power_k = 1
# Calculate power^(k-1) mod modulo
for _ in range(k - 1):
power_k = (power_k * power) % modulo
# Calculate initial hash for the first substring
for i in range(k):
current_hash = (current_hash * power + (ord(s[i]) - ord('a') + 1)) % modulo
if current_hash == hashValue:
return s[:k]
# Iterate through the remaining substrings using rolling hash
for i in range(k, n):
current_hash = (current_hash - (ord(s[i - k]) - ord('a') + 1) * power_k) % modulo
if current_hash < 0:
current_hash += modulo # Ensure non-negative value
current_hash = (current_hash * power + (ord(s[i]) - ord('a') + 1)) % modulo
if current_hash == hashValue:
return s[i - k + 1:i + 1]
return "" # Should not reach here based on the problem constraints