Hash Divided String - Problem
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.
Process:
1. First, divide s into n / k substrings, each with a length of k.
2. Initialize result as an empty string.
3. 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.
Input & Output
Example 1 — Basic Case
$
Input:
s = "abcd", k = 2
›
Output:
"bf"
💡 Note:
Substring "ab": hash sum = 0 + 1 = 1, 1 % 26 = 1 → 'b'. Substring "cd": hash sum = 2 + 3 = 5, 5 % 26 = 5 → 'f'. Result: "bf"
Example 2 — Single Character Groups
$
Input:
s = "xyz", k = 1
›
Output:
"xyz"
💡 Note:
Each character forms its own substring: 'x' → 23 % 26 = 23 → 'x', 'y' → 24 % 26 = 24 → 'y', 'z' → 25 % 26 = 25 → 'z'
Example 3 — Wraparound Case
$
Input:
s = "zzab", k = 2
›
Output:
"zb"
💡 Note:
Substring "zz": hash sum = 25 + 25 = 50, 50 % 26 = 24 → 'y'. Wait, let me recalculate: 50 % 26 = 24 → 'y'. Actually "ab": 0 + 1 = 1 → 'b'. So result is "yb"
Constraints
- 1 ≤ s.length ≤ 1000
- 1 ≤ k ≤ s.length
- s.length is a multiple of k
- s consists of lowercase English letters only
Visualization
Tap to expand
Understanding the Visualization
1
Input
String s and block size k
2
Process
Divide into k-length blocks and hash each
3
Output
Hashed result string
Key Takeaway
🎯 Key Insight: Each k-length substring can be independently hashed by summing character positions modulo 26
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code