Loading...
Loading...
You are given a string moves that describes n objects placed at distinct points on a straight line. The objects are numbered from 0 to n - 1 from left to right, and moves[i] says what object i does:
'L' means the object moves to the left.'R' means the object moves to the right.'S' means the object stands still.Every moving object travels at the same speed. When two objects meet, they collide and the collision adds to a score:
2.1.After a collision, every object involved stops for good and stays at the point where it collided. A stopped object counts as standing still for any later collision. Apart from that, no object ever changes what it is doing.
Return the total score of all collisions that happen.
The objects are drawn evenly spaced.
Input: moves = "RLRSLL"
Output: 5
Explanation: Objects 0 and 1 move toward each other, so they add 2. Object 2 hits object 3, which stands still, and adds 1. Object 4 also hits object 3 and adds 1, then stops there. Object 5 hits the stopped object 4 and adds 1. The total is 2 + 1 + 1 + 1 = 5.
Input: moves = "LLRR"
Output: 0
Explanation: Objects 0 and 1 both move left at the same speed, so they never meet. Objects 2 and 3 both move right, away from everything else. No collision happens, so the score is 0.
moves.length ≤105moves[i] is 'L', 'R', or 'S'.