Line Up
Description
To prepare his students for an upcoming game, the sports coach decides to try some new training drills. To begin with, he lines them up and starts with the following warm-up exercise: when the coach says 'L'
, he instructs the students to turn to the left. Alternatively, when he says 'R'
, they should turn to the right. Finally, when the coach says 'A'
, the students should turn around.
Unfortunately some students (not all of them, but at least one) can’t tell left from right, meaning they always turn right when they hear 'L'
and left when they hear 'R'
. The coach wants to know how many times the students end up facing the same direction.
Given the list of commands the coach has given, count the number of such commands after which the students will be facing the same direction.
Example
For commands = "LLARL"
, the output should be
lineUp(commands) = 3
.
Let’s say that there are 4
students, and the second one can’t tell left from right. In this case, only after the second, third and fifth commands will the students face the same direction.
Input/Output
-
[execution time limit] 4 seconds (js)
-
[input] string commands
String consisting of characters
'L'
,'R'
and'A'
only.Guaranteed constraints:
0 \leq commands.length \leq 35
. -
[output] integer
- The number of commands after which students face the same direction.
[JavaScript (ES6)] Syntax Tips
1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
function lineUp(commands) {
var c = {
L: 1,
R: 3,
A: 2
};
var d = {
R: 1,
L: 3,
A: 2
};
var e = 0;
var f = 0;
var ret = 0;
for (var i = 0; i < commands.length; i++) {
var cmd = commands[i];
e = (e + c[cmd]) % 4;
f = (f + d[cmd]) % 4;
if (e === f) ret++;
}
return ret;
}