Problem Statement
You are in charge of assigning courts for a singles tennis tournament. There are two kinds of matches: female matches and male matches. Each court must be used for exactly one kind of match. You passed the task to your assistant, but unfortunately, you forgot that there was one additional rule: adjacent courts cannot be assigned the same kind of match. Your assistant was also unaware of this rule when he did the assignments, so now you must correct them.
There are N courts arranged in a single row, numbered 0 to N-1. You are given a String assignment, where the i-th character represents the kind of match assigned to court i by your assistant. 'F' represents a female match and 'M' represents a male match. You must change some of the courts to the opposite kind of match in order to satisfy the rules. Return the minimum number of courts that need to be changed.
Definition
- Class:
- TennisCourts
- Method:
- minChanges
- Parameters:
- String
- Returns:
- int
- Method signature:
- int minChanges(String assignment)
- (be sure your method is public)
Constraints
- assignment will contain between 1 and 50 characters, inclusive.
- Each character of assignment will be either an uppercase 'M' or an uppercase 'F'.
Examples
"FFMMF"
Returns: 2
You can change the assignment of the third and fourth courts to get "FMFMF" with a total of 2 changes."MFMFMF"
Returns: 0
The assignment is already correct because there are no two adjacent courts assigned to the same kind of match."FFFFFFFF"
Returns: 4
"FFMFMFMFMFMFMFFMMMMFMFMFMFMFMMMFMFMFMFMFMFMMMMF"
Returns: 8