-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0696-count-binary-substrings.js
More file actions
39 lines (34 loc) · 963 Bytes
/
0696-count-binary-substrings.js
File metadata and controls
39 lines (34 loc) · 963 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
35
36
37
38
39
/**
* Count Binary Substrings
* Time Complexity: O(N)
* Space Complexity: O(N)
*/
var countBinarySubstrings = function (s) {
let firstPointer = 0;
const stringLength = s.length;
const groupBlockLengths = [];
while (firstPointer < stringLength) {
const currentCharacterValue = s[firstPointer];
let currentBlockSize = 0;
let secondPointer = firstPointer;
while (
secondPointer < stringLength &&
s[secondPointer] === currentCharacterValue
) {
currentBlockSize++;
secondPointer++;
}
groupBlockLengths.push(currentBlockSize);
firstPointer = secondPointer;
}
let finalCount = 0;
const numGroups = groupBlockLengths.length;
let groupIndex = 0;
while (groupIndex < numGroups - 1) {
const lengthOne = groupBlockLengths[groupIndex];
const lengthTwo = groupBlockLengths[groupIndex + 1];
finalCount += Math.min(lengthOne, lengthTwo);
groupIndex++;
}
return finalCount;
};