1759. Count Number of Homogenous Substrings
T: O(n)
S: O(1)
```java
class Solution {
public int countHomogenous(String s) {
int mod = 1000000007;
long count = 0;
long result = 0;
char pre = '0';
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c != pre) {
count = 1;
pre = c;
} else {
count++;
}
result += count;
}
return (int)(result % mod);
}
}
/***
subarry -> homogenous ->
這題就分開來計算, 看subarray 是 homogenous 就可以了, 算這個 subarray 能產生的結果總數 (ex: ccc -> 6 種可能, c 3種, cc 2種, ccc 1種 = 3+2+1)
a = 1
bb = 3
ccc = 6
aa = 3
so 1+3+6+3 = 13
ccc = 3+2+1
bb = 2+1
a = 1
zzzzz =
5+4+3+2+1
so just accumulate cuurent count (for this char)
T: O(n)
S: O(1)
*/
```
Last updated