3271. Hash Divided String LeetCode Solution

In this guide, you will get 3271. Hash Divided String LeetCode Solution with the best time and space complexity. The solution to Hash Divided String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Hash Divided String solution in C++
  4. Hash Divided String solution in Java
  5. Hash Divided String solution in Python
  6. Additional Resources
3271. Hash Divided String LeetCode Solution image

Problem Statement of Hash Divided String

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 <= 100
k <= s.length <= 1000
s.length is divisible by k.
s consists only of lowercase English letters.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

3271. Hash Divided String LeetCode Solution in C++

class Solution {
 public:
  string stringHash(string s, int k) {
    string ans;

    for (int i = 0; i < s.length(); i += k) {
      int sumHash = 0;
      for (int j = i; j < i + k; ++j)
        sumHash += s[j] - 'a';
      ans += 'a' + sumHash % 26;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

3271. Hash Divided String LeetCode Solution in Java

class Solution {
  public String stringHash(String s, int k) {
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < s.length(); i += k) {
      int sumHash = 0;
      for (int j = i; j < i + k; ++j)
        sumHash += s.charAt(j) - 'a';
      sb.append((char) ('a' + sumHash % 26));
    }

    return sb.toString();
  }
}
// code provided by PROGIEZ

3271. Hash Divided String LeetCode Solution in Python

class Solution:
  def stringHash(self, s: str, k: int) -> str:
    ans = []

    for i in range(0, len(s), k):
      sumHash = sum(string.ascii_lowercase.index(s[j])
                    for j in range(i, i + k))
      ans.append(string.ascii_lowercase[sumHash % 26])

    return ''.join(ans)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.