We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e0b6989 commit 704e7c9Copy full SHA for 704e7c9
Medium/Count Number of Homogenous Substrings.java
@@ -1,18 +1,18 @@
1
class Solution {
2
- private final int MOD = 1000000007;
3
- public int countHomogenous(String s) {
4
- int count = 0;
5
- int start = 0;
6
- int end = 0;
7
- int n = s.length();
8
- while (end < n) {
9
- if (s.charAt(start) == s.charAt(end)) {
10
- count = (count + (end - start + 1)) % MOD;
11
- end++;
12
- } else {
13
- start++;
14
- }
+
+ private static final int MOD = 1_000_000_007;
+ public int countHomogenous(String s) {
+ int result = 0;
+ int streak = 0;
+ for (int i = 0; i < s.length(); i++) {
+ if (i == 0 || s.charAt(i) == s.charAt(i - 1)) {
+ streak++;
+ } else {
+ streak = 1;
+ }
+ result = (result + streak) % MOD;
15
16
+ return result;
17
}
- return count;
18
0 commit comments