-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.js
64 lines (56 loc) · 1.32 KB
/
Solution.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @param {string} s
* @return {number}
*/
var countBinarySubstrings = function(s) {
var ans = 0, i, m, n, length = s.length - 1;
for (i = 0; i < length; i++) {
if (s[i] === s[i+1]) continue;
ans++;
m = i - 1;
n = i + 2;
while(m >= 0 && n <= length) {
if (s[m] !== s[i] || s[n] !== s[i+1]) break;
m--;
n++;
ans++;
}
}
return ans;
};
/**
* @param {string} s
* @return {number}
*/
var countBinarySubstrings = function(s) {
var ans = 0, i, length = s.length, group = [1], prev = s[0];
for (i = 1; i < length; i++) {
if (s[i] === prev) {
group[group.length - 1]++;
} else {
group.push(1);
prev = s[i];
}
}
for (i = 1; i < group.length; i++) ans += Math.min(group[i-1], group[i]);
return ans;
};
/**
* @param {string} s
* @return {number}
*/
var countBinarySubstrings = function(s) {
var ans = 0, i, length = s.length, prev = 0, cur = 1, prevStr = s[0];
for (i = 1; i < length; i++) {
if (s[i] === prevStr) {
cur++;
} else {
prevStr = s[i];
ans += Math.min(prev, cur);
prev = cur;
cur = 1;
}
}
ans += Math.min(prev, cur);
return ans;
};