Skip to content

Commit fb30488

Browse files
authored
Create count-number-of-homogenous-substrings.cpp
1 parent 11a61ca commit fb30488

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(n)
2+
// Space; O(1)
3+
4+
class Solution {
5+
public:
6+
int countHomogenous(string s) {
7+
static const int MOD = 1e9 + 7;
8+
int cnt = 0, result = 0;
9+
for (int i = 0; i < size(s); ++i) {
10+
if (i > 0 && s[i - 1] == s[i]) {
11+
++cnt;
12+
} else {
13+
cnt = 1;
14+
}
15+
result = (result + cnt) % MOD;
16+
}
17+
return result;
18+
}
19+
};

0 commit comments

Comments
 (0)