Skip to content

Commit 704e7c9

Browse files
authored
Update Count Number of Homogenous Substrings.java
1 parent e0b6989 commit 704e7c9

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
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-
}
2+
3+
private static final int MOD = 1_000_000_007;
4+
5+
public int countHomogenous(String s) {
6+
int result = 0;
7+
int streak = 0;
8+
for (int i = 0; i < s.length(); i++) {
9+
if (i == 0 || s.charAt(i) == s.charAt(i - 1)) {
10+
streak++;
11+
} else {
12+
streak = 1;
13+
}
14+
result = (result + streak) % MOD;
15+
}
16+
return result;
1517
}
16-
return count;
17-
}
1818
}

0 commit comments

Comments
 (0)