Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ae02d4a

Browse files
committed
Add solution #1759
1 parent efd66ff commit ae02d4a

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,524 LeetCode solutions in JavaScript
1+
# 1,525 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1354,6 +1354,7 @@
13541354
1754|[Largest Merge Of Two Strings](./solutions/1754-largest-merge-of-two-strings.js)|Medium|
13551355
1755|[Closest Subsequence Sum](./solutions/1755-closest-subsequence-sum.js)|Hard|
13561356
1758|[Minimum Changes To Make Alternating Binary String](./solutions/1758-minimum-changes-to-make-alternating-binary-string.js)|Easy|
1357+
1759|[Count Number of Homogenous Substrings](./solutions/1759-count-number-of-homogenous-substrings.js)|Medium|
13571358
1764|[Form Array by Concatenating Subarrays of Another Array](./solutions/1764-form-array-by-concatenating-subarrays-of-another-array.js)|Medium|
13581359
1765|[Map of Highest Peak](./solutions/1765-map-of-highest-peak.js)|Medium|
13591360
1768|[Merge Strings Alternately](./solutions/1768-merge-strings-alternately.js)|Easy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 1759. Count Number of Homogenous Substrings
3+
* https://leetcode.com/problems/count-number-of-homogenous-substrings/
4+
* Difficulty: Medium
5+
*
6+
* Given a string s, return the number of homogenous substrings of s. Since the answer may be
7+
* too large, return it modulo 109 + 7.
8+
*
9+
* A string is homogenous if all the characters of the string are the same.
10+
*
11+
* A substring is a contiguous sequence of characters within a string.
12+
*/
13+
14+
/**
15+
* @param {string} s
16+
* @return {number}
17+
*/
18+
var countHomogenous = function(s) {
19+
const mod = 1e9 + 7;
20+
let result = 0;
21+
let streak = 1;
22+
23+
for (let i = 1; i < s.length; i++) {
24+
if (s[i] === s[i - 1]) {
25+
streak++;
26+
} else {
27+
result = (result + (streak * (streak + 1) / 2)) % mod;
28+
streak = 1;
29+
}
30+
}
31+
32+
result = (result + (streak * (streak + 1) / 2)) % mod;
33+
34+
return result;
35+
};

0 commit comments

Comments
 (0)