Simulation String 3271. Hash Divided String ¶ Time: $O(n)$ Space: $O(n)$ C++JavaPython 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15class 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; } }; 1 2 3 4 5 6 7 8 9 10 11 12 13 14class 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(); } } 1 2 3 4 5 6 7 8 9 10class 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)