Skip to main content
Back to problems
#3271
Medium Algorithms

Hash divided string

String Simulation
83.2% acceptance
Feb 25, 2026
108
15
You are given a string s and an integer k. Divide into n/k substrings of length k. For each substring: sum char values (a=0..z=25), take sum%26, output that char. Return result.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn string_hash(s: String, k: i32) -> String {
    let k = k as usize;
    let bytes = s.as_bytes();
    let m = bytes.len() / k;
    let mut result = Vec::with_capacity(m);
    for chunk in bytes.chunks(k) {
      let sum: u32 = chunk.iter().map(|&b| (b - b'a') as u32).sum();
      result.push(b'a' + (sum % 26) as u8);
    }
    String::from_utf8(result).unwrap()
  }
}