3271. Hash Divided String Medium

@problem@discussion
#String#Simulation



1/**
2 * [3271] Hash Divided String
3 *
4 * 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.
5 * First, divide s into n / k <span data-keyword="substring-nonempty">substrings</span>, each with a length of k. Then, initialize result as an empty string.
6 * For each substring in order from the beginning:
7 * 
8 * 	The hash value of a character is the index of that characte<!-- notionvc: 4b67483a-fa95-40b6-870d-2eacd9bc18d8 -->r in the English alphabet (e.g., 'a' &rarr;<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 0, 'b' &rarr;<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 1, ..., 'z' &rarr;<!-- notionvc: d3f8e4c2-23cd-41ad-a14b-101dfe4c5aba --> 25).
9 * 	Calculate the sum of all the hash values of the characters in the substring.
10 * 	Find the remainder of this sum when divided by 26, which is called hashedChar.
11 * 	Identify the character in the English lowercase alphabet that corresponds to hashedChar.
12 * 	Append that character to the end of result.
13 * 
14 * Return result.
15 *  
16 * <strong class="example">Example 1:
17 * <div class="example-block">
18 * Input: <span class="example-io">s = "abcd", k = 2</span>
19 * Output: <span class="example-io">"bf"</span>
20 * Explanation:
21 * First substring: "ab", 0 + 1 = 1, 1 % 26 = 1, result[0] = 'b'.
22 * Second substring: "cd", 2 + 3 = 5, 5 % 26 = 5, result[1] = 'f'.
23 * </div>
24 * <strong class="example">Example 2:
25 * <div class="example-block">
26 * Input: <span class="example-io">s = "mxz", k = 3</span>
27 * Output: <span class="example-io">"i"</span>
28 * Explanation:
29 * The only substring: "mxz", 12 + 23 + 25 = 60, 60 % 26 = 8, result[0] = 'i'.
30 * </div>
31 *  
32 * Constraints:
33 * 
34 * 	1 <= k <= 100
35 * 	k <= s.length <= 1000
36 * 	s.length is divisible by k.
37 * 	s consists only of lowercase English letters.
38 * 
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/hash-divided-string/
43// discuss: https://leetcode.com/problems/hash-divided-string/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47impl Solution {
48    pub fn string_hash(s: String, k: i32) -> String {
49        String::new()
50    }
51}
52
53// submission codes end
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn test_3271() {
61    }
62}
63


Back
© 2025 bowen.ge All Rights Reserved.