Problem Statement
Your task is, given a String describing people going up the stairs, return the number of people who are not true gentlemen. In more detail, the input String, read from left to right, will describe the order in which events happen on the stairs. The String will consist of characters 'L', 'G' and ' ' (space), where 'L' indicates a lady going up the stairs, 'G' indicates a gentleman going up the stairs and space (' ') characters indicate periods of time when nobody goes up the stairs (that is, the spaces divide one group of people from another). You need to count the number of "gentlemen" that allow a lady from the same group to go up the stairs before them.
Definition
- Class:
- Etiquette
- Method:
- numNonGent
- Parameters:
- String
- Returns:
- int
- Method signature:
- int numNonGent(String people)
- (be sure your method is public)
Notes
- Gaps between groups are assumed to be long enough. Hence, a gentleman who goes up the stairs later than a lady from a previous group doesn't necessarily count as not being a true gentleman (his status depends on his position relative the ladies in his own group).
Constraints
- people will consist only of the characters 'L', 'G' and ' ' (space)
- people will have between 0 and 50 characters inclusive
Examples
"LGG"
Returns: 2
"GGL LGG"
Returns: 2
"GLL LL GG G GGLL"
Returns: 0
""
Returns: 0
"LLL GGG"
Returns: 0
"LL GG GLG GGL LG LG GL"
Returns: 3
The first two groups consist of people of one gender, so there is no way to check if they know rules, hence we do not count anyone here. In the third group one gentleman doesn't know the exception rule, hence we count one non-true gentleman here. In the fourth group both gentlemen are true gentlemen, so we add zero here. In the fifth the gentleman is not a true gentleman, so we add one. The sixth group is the same as the fifth group, so we add one more. In the last group the gentleman is a true gentleman, so we do not count him.