Skip to content

Commit a8af189

Browse files
authored
Create hash-divided-string.cpp
1 parent 976e98c commit a8af189

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

C++/hash-divided-string.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// string
5+
class Solution {
6+
public:
7+
string stringHash(string s, int k) {
8+
string result(size(s) / k, 'a');
9+
for (int i = 0; i < size(s); i += k) {
10+
int curr = 0;
11+
for (int j = 0; j < k; ++j) {
12+
curr = (curr + (s[i + j] - 'a')) % 26;
13+
}
14+
result[i / k] += curr;
15+
}
16+
return result;
17+
}
18+
};

0 commit comments

Comments
 (0)