Skip to content

Commit ca9d4ff

Browse files
committed
Add solution #696
1 parent b496402 commit ca9d4ff

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@
290290
684|[Redundant Connection](./0684-redundant-connection.js)|Medium|
291291
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|
292292
695|[Max Area of Island](./0695-max-area-of-island.js)|Medium|
293+
696|[Count Binary Substrings](./0696-count-binary-substrings.js)|Easy|
293294
697|[Degree of an Array](./0697-degree-of-an-array.js)|Easy|
294295
700|[Search in a Binary Search Tree](./0700-search-in-a-binary-search-tree.js)|Easy|
295296
701|[Insert into a Binary Search Tree](./0701-insert-into-a-binary-search-tree.js)|Medium|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 696. Count Binary Substrings
3+
* https://leetcode.com/problems/count-binary-substrings/
4+
* Difficulty: Easy
5+
*
6+
* Given a binary string s, return the number of non-empty substrings that have the same
7+
* number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped
8+
* consecutively.
9+
*
10+
* Substrings that occur multiple times are counted the number of times they occur.
11+
*/
12+
13+
/**
14+
* @param {string} s
15+
* @return {number}
16+
*/
17+
var countBinarySubstrings = function(s) {
18+
let result = 0;
19+
20+
for (let i = 0, group = [], count = 1, prevCount = 0; i < s.length; i++) {
21+
if (s[i] === s[i + 1]) {
22+
count++;
23+
} else {
24+
if (prevCount) {
25+
result += prevCount <= count ? prevCount : count;
26+
}
27+
prevCount = count;
28+
count = 1;
29+
}
30+
}
31+
32+
return result;
33+
};

0 commit comments

Comments
 (0)