Hash Divided String Problem
#3271
Try it yourself first
Five languages, sample and hidden tests, right in the browser.
Practice it as an interview
Talk through your solution with an AI interviewer while you code.
Description
LeetCode Problem 3271.
You are given a string s of length n and an integer k, where n is a multiple of k. Your task is to hash the string s into a new string called result, which has a length of n / k.
First, divide s into n / k substrings, each with a length of k. Then, initialize result as an empty string.
For each substring in order from the beginning:
- The hash value of a character is the index of that character in the English alphabet (e.g.,
'a' → 0,'b' → 1, ...,'z' → 25). - Calculate the sum of all the hash values of the characters in the substring.
- Find the remainder of this sum when divided by 26, which is called
hashedChar. - Identify the character in the English lowercase alphabet that corresponds to
hashedChar. - Append that character to the end of
result.
Return result.
Example 1:
Input: s = "abcd", k = 2
Output: "bf"
Explanation:
First substring: "ab", 0 + 1 = 1, 1 % 26 = 1, result[0] = 'b'.
Second substring: "cd", 2 + 3 = 5, 5 % 26 = 5, result[1] = 'f'.
Example 2:
Input: s = "mxz", k = 3
Output: "i"
Explanation:
The only substring: "mxz", 12 + 23 + 25 = 60, 60 % 26 = 8, result[0] = 'i'.
Constraints:
1 <= k <= 100k <= s.length <= 1000s.lengthis divisible byk.sconsists only of lowercase English letters.
Solution
Approach: Simulation
Cut s into blocks of k characters. For each block add up the letter values (a = 0, ..., z = 25), take the sum modulo 26 and append the letter with that value to the result.
Complexity: O(n) time and O(n/k) space for the result.
class Solution:
def stringHash(self, s: str, k: int) -> str:
ans = []
for i in range(0, len(s), k):
t = 0
for j in range(i, i + k):
t += ord(s[j]) - ord("a")
hashedChar = t % 26
ans.append(chr(ord("a") + hashedChar))
return "".join(ans)