-
Notifications
You must be signed in to change notification settings - Fork 42
/
solution.ts
34 lines (34 loc) · 1012 Bytes
/
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
function addMinimum (word: string): number {
let result = 0;
let index = 0;
while (index < word.length) {
if (word[index] === 'a') {
if (index + 1 < word.length && word[index + 1] === 'b') {
if (index + 2 < word.length && word[index + 2] === 'c') {
index += 3;
} else {
result++;
index += 2;
}
} else if (index + 1 < word.length && word[index + 1] === 'c') {
result += 1;
index += 2;
} else {
result += 2;
index++;
}
} else if (word[index] === 'b') {
if (index + 1 < word.length && word[index + 1] === 'c') {
result++;
index += 2;
} else {
result += 2;
index++;
}
} else {
result += 2;
index++;
}
}
return result;
}