-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathString doubles.js
29 lines (24 loc) · 1.01 KB
/
String doubles.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
/*
Description:
In this Kata, you will write a function doubles that will remove double string characters that are adjacent to each other.
For example:
doubles('abbcccdddda') = 'aca', because, from left to right:
a) There is only one 'a' on the left hand side, so it stays.
b) The 2 b's disapper because we are removing double characters that are adjacent.
c) Of the 3 c's, we remove two. We are only removing doubles.
d) The 4 d's all disapper, because we first remove the first double, and again we remove the second double.
e) There is only one 'a' at the end, so it stays.
Two more examples: doubles('abbbzz') = 'ab' and doubles('abba') = "". In the second example, when we remove the b's in 'abba', the double a that results is then removed.
The strings will contain lowercase letters only. More examples in the test cases.
Good luck!
*/
function doubles(s) {
const cs = [];
for (const c of s) {
if (cs.length != 0 && cs[cs.length - 1] == c)
cs.pop();
else
cs.push(c);
}
return cs.join('');
}