-
Notifications
You must be signed in to change notification settings - Fork 42
/
solution.ts
45 lines (44 loc) · 1.17 KB
/
solution.ts
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
function minimumBuckets (street: string): number {
let result = 0;
let lastBucketIndex = -1;
let index = 0;
while (index < street.length) {
if (street[index] === '.') {
index++;
continue;
}
const start = index++;
while (index < street.length && street[index] === 'H') {
index++;
}
const count = index - start;
if (count > 2) {
return -1;
}
if (count === 1) {
if (start === 0) {
if (index === street.length) {
return -1;
}
lastBucketIndex = index;
result++;
continue;
}
if (lastBucketIndex === start - 1) {
continue;
}
lastBucketIndex = index;
result++;
} else if (count === 2) {
if (index === street.length || start === 0) {
return -1;
}
if (lastBucketIndex !== start - 1) {
result++;
}
result++;
lastBucketIndex = index;
}
}
return result;
}