Skip to content

Commit c2cc6f9

Browse files
committed
solved Daily Challenge , count number of homogenous sub strings
1 parent 34ff0f6 commit c2cc6f9

File tree

1 file changed

+57
-0
lines changed
  • LeetCode/CountNumberofHomogenousSubstrings

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <bits/stdc++.h>
2+
#include <gtest/gtest.h>
3+
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
int countHomogenous(string s) {
9+
if (s.empty()) return 0;
10+
long ret = 0;
11+
int len = s.size();
12+
int lasti = 0;
13+
char last = s[0];
14+
for (int i = 1; i < len; i++) {
15+
if (s[i] == last) {
16+
continue;
17+
} else {
18+
ret += cal(i - lasti);
19+
//
20+
last = s[i];
21+
lasti = i;
22+
}
23+
}
24+
ret += cal(len - lasti);
25+
return ret % long(1e9 + 7);
26+
}
27+
private:
28+
long cal(int n) {
29+
return long(1 + n) * n / 2;
30+
}
31+
};
32+
33+
struct T {
34+
35+
};
36+
37+
TEST(Solution, test) {
38+
T ts[] = {
39+
{
40+
41+
},
42+
43+
};
44+
45+
for (T t : ts) {
46+
Solution solution;
47+
48+
}
49+
}
50+
51+
int main() {
52+
testing::InitGoogleTest();
53+
54+
return RUN_ALL_TESTS();
55+
}
56+
57+

0 commit comments

Comments
 (0)