forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount Binary Substrings.js
47 lines (45 loc) · 1.16 KB
/
Count Binary Substrings.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* find all bit switches '01' and '10'.
* From each one expand sideways: i goes left, j goes right
* Until:
* if '01' -> i,j != 0,1
* if '10' -> i,j != 1,0
* and within input boundaries
*/
var countBinarySubstrings = function(s) {
let i = 0;
const n = s.length;
let count = 0;
while (i < n-1) {
if (s[i] != s[i+1]) {
if (s[i] === '0') {
count += countZeroOnes(s, i, true);
} else {
count += countZeroOnes(s, i, false);
}
}
i++;
}
return count;
// count the number of valid substrings substrings
function countZeroOnes(s, start, startsWithZero) {
let count = 0;
let i = start;
let j = start+1;
const n = s.length;
if (startsWithZero) {
while(i >= 0 && j < n && s[i] === '0' && s[j] === '1') {
count++;
i--;
j++;
}
} else {
while(i >= 0 && j < n && s[i] === '1' && s[j] === '0') {
count++;
i--;
j++;
}
}
return count;
}
}