-
Notifications
You must be signed in to change notification settings - Fork 1
/
alternatingCharacters.js
44 lines (34 loc) · 948 Bytes
/
alternatingCharacters.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
/**
Complete the alternatingCharacters function in the editor below. It must return an integer representing the minimum number of deletions to make the alternating string.
Output Format
For each query, print the minimum number of deletions required on a new line.
Sample Input
5
AAAA
BBBBB
ABABABAB
BABABA
AAABBB
Sample Output
3
4
0
0
4
*/
const alternatingCharacters = (s) => {
let count = 0;
for (let i = 0; i < s.length - 1; i++) {
const currChar = s.charAt(i);
const nextChar = s.charAt(i + 1);
if (currChar === nextChar) {
count++;
}
}
return count;
};
console.log(alternatingCharacters("AAAA")); // A___ = 3 Deletions
console.log(alternatingCharacters("BBBBB")); // B____ = 4 Deletions
console.log(alternatingCharacters("ABABABAB")); // ABABABAB = 0 Deletions
console.log(alternatingCharacters("BABABA")); // BABABA = 0 Deletions
console.log(alternatingCharacters("AAABBB")); // A__B__ = 4 Deletions