Connection Status:
Competition Arena > LeftAndRightHandedDiv2
SRM 612 · 2013-12-22 · by ltaravilse · Greedy, Simple Search, Iteration
Class Name: LeftAndRightHandedDiv2
Return Type: int
Method Name: count
Arg Types: (string)
Problem Statement

Problem Statement

Some students are seated in a row next to each other. Each of them is either left-handed or right-handed. You are given a String S that describes the row of students. Each character of S is either 'L' or 'R', representing a left-handed or a right-handed person, respectively. The characters describe the row from the left to the right: for all i, the person described by character i+1 sits to the right of the person described by character i.

The students are trying to write down lecture notes. Whenever a left-handed person sits immediately to the right of a right-handed person, their elbows collide when they both try to write at the same time. Compute and return the number of elbow collisions, assuming that all students in the row attempt to write at the same time.

Constraints

  • S will contain between 1 and 50 characters, inclusive.
  • Each character of S will be either 'L' or 'R'.
Examples
0)
"L"
Returns: 0

There's only one person in the row so there are no collisions.

1)
"RRR"
Returns: 0

Everybody is right-handed so there are no collisions.

2)
"LRLRLR"
Returns: 2

There will be two collisions: one of them between the second and the third person from the left (described by S[1] and S[2]) and the other between the fourth and the fifth person.

3)
"LLLRRR"
Returns: 0
4)
"RLRLRL"
Returns: 3

Submissions are judged against all 41 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class LeftAndRightHandedDiv2 with a public method int count(string S) · 41 test cases · 2 s / 256 MB per case

Submitting as anonymous