Count Collisions on a Road LeetCode solution in JavaScript

1
/**
2
* @param {string} directions
3
* @return {number}
4
*/
5
var countCollisions = function (directions) {
6
if (directions.length == 1) {
7
return 0;
8
}
9
let count = 0;
10
let couplet;
11
let retreivedWieght;
12
let convertedArr = [];
13
let rightMovingCount = 0;
14
let weights = new Map();
15
weights.set("LR", 0);
16
weights.set("RL", 2);
17
weights.set("LS", 0);
18
weights.set("SL", 1);
19
weights.set("SR", 0);
20
weights.set("RS", 1);
21
weights.set("LL", 0);
22
weights.set("RR", 0);
23
weights.set("SS", 0);
24

25
for (let i = 0; i < directions.length; i++) {
26
convertedArr[i] = directions[i];
27
}
28

29
for (let i = 1; i < convertedArr.length; i++) {
30
couplet = convertedArr[i - 1] + convertedArr[i];
31
retreivedWieght = weights.get(couplet);
32
count = count + retreivedWieght;
33
if (retreivedWieght > 0) {
34
convertedArr[i - 1] = "S";
35
convertedArr[i] = "S";
36
if (rightMovingCount > 0) {
37
count = count + rightMovingCount;
38
rightMovingCount = 0;
39
}
40
}
41
if (couplet === "RR") {
42
rightMovingCount++;
43
}
44
}
45
return count;
46
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0