-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
34 lines (33 loc) · 773 Bytes
/
solution.js
File metadata and controls
34 lines (33 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* @param {string} s
* @return {number}
*/
var countBinarySubstrings = function(s) {
let zeroCount = 0,
oneCount = 0,
count = 0,
lastCharacter = null
s.split('').forEach(c => {
if (c === '0') {
if (lastCharacter != '0') {
zeroCount = 1
} else {
zeroCount += 1
}
if (zeroCount <= oneCount) {
count += 1
}
} else if (c === '1') {
if (lastCharacter != '1') {
oneCount = 1
} else {
oneCount += 1
}
if (oneCount <= zeroCount) {
count += 1
}
}
lastCharacter = c
})
return count
};