Problem Statement
Now Camomile wants to put the creatures back in order. The creatures are considered to be in order if the white creatures all sit in cages with numbers from 1 to W, where W is the total number of white creatures, and the black creatures all sit in cages with numbers from W+1 to N. To achieve the necessary order, Camomile can use N different spells, also numbered from 1 to N. The spell with number i can swap creatures from two cages if the distance between those cages is exactly equal to i. The distance between cages with numbers a and b is calculated as |a - b|. She can use each spell at most once and each spell can swap only one pair of creatures.
Return the minimum number of spells that she needs to use in order to put creatures back in order. If it is impossible, return -1.
Definition
- Class:
- BlackWhiteMagic
- Method:
- count
- Parameters:
- String
- Returns:
- int
- Method signature:
- int count(String creatures)
- (be sure your method is public)
Constraints
- creatures will contain between 1 and 50 characters, inclusive.
- Each character in creatures will be either 'B' or 'W'.
Examples
"WBBW"
Returns: 1
By using the second spell and swapping the creatures in the second and last cages we will obtain the required order: "WWBB".
"WWWWBBBB"
Returns: 0
Here all creatures are already in the required order, so no spells are needed.
"BBWW"
Returns: 2
One possible way is to start with swapping the first and third creatures: BBWW -> [spell 2 on creatures 1 and 3] -> WBBW WBBW -> [spell 1 on creatures 1 and 2] -> BWBW BWBW -> [spell 3 on creatures 1 and 4] -> WWBB However, a better solution can be obtained if you start with swapping the second and third creatures: BBWW -> [spell 1 on creatures 2 and 3] -> BWBW BWBW -> [spell 3 on creatures 1 and 4] -> WWBB
"BWWWWWWWBBBBBBBW"
Returns: 1
"WBWBWBWBWWBWBWBWBBBWBW"
Returns: 5
"B"
Returns: 0
"W"
Returns: 0
"BB"
Returns: 0
"WB"
Returns: 0
"BW"
Returns: 1
"WW"
Returns: 0
"BBB"
Returns: 0
"WBB"
Returns: 0
"BWB"
Returns: 1
"WWB"
Returns: 0
"BBW"
Returns: 1
"WBW"
Returns: 1
"BWW"
Returns: 1
"WWW"
Returns: 0
"BBBB"
Returns: 0
"WBBB"
Returns: 0
"BWBB"
Returns: 1
"WWBB"
Returns: 0
"BBWB"
Returns: 1
"WBWB"
Returns: 1
"BWWB"
Returns: 1
"WWWB"
Returns: 0
"BBBW"
Returns: 1
"WBBW"
Returns: 1
"BWBW"
Returns: 1
"WWBW"
Returns: 1
"BBWW"
Returns: 2
"WBWW"
Returns: 1
"BWWW"
Returns: 1
"WWWW"
Returns: 0
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"
Returns: 0
"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
Returns: 0
"BWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBW"
Returns: 13
"WBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWBWB"
Returns: 12
"BWBWBWBWBWBWBWBWBWBWBWBWBBWBWBWBWBWBWBWBWBWBWBWBWB"
Returns: 12
"WBWBWBWBWBWBWBWBWBWBWBWBWWBWBWBWBWBWBWBWBWBWBWBWBW"
Returns: 12
"BWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"
Returns: 1
"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBW"
Returns: 1
"BBBBBBBBBBBBBBBBBBBBBBBBBWWWWWWWWWWWWWWWWWWWWWWWWW"
Returns: 25
"WWWWWWWWWWWWWWWWWWWWWWWWWBBBBBBBBBBBBBBBBBBBBBBBBB"
Returns: 0
"WBBWBBBBBWWWWWWWBWBWBBWBBWBBWWBWBWBWWWBWWB"
Returns: 11
"WBWWWBWBBWWWWWWBBWBBBBBBBBBWBWBBBW"
Returns: 4
"BWWBBBBBBWBBWBWWBBBWWWWWBBBWBWBWWBBBWWWWBBBW"
Returns: 13
"WWWBWBBBWBBBWWWWWWWWWWWBWBBBBBWBBWBWB"
Returns: 7
"BWBWWWBBWB"
Returns: 2
"BBWBWBBWBWWBBBBWWBWBWWWBW"
Returns: 7
"BWBWWBWWBBWBBB"
Returns: 3
"WBWWWWWWBW"
Returns: 1
"BBBBBBWWBWWBBBBBWBWBWWBBWBBBWBBBBWWWBBBWBBWWWWBBW"
Returns: 13
"BBBWWWBWWWWBWBWBBWWBBWBWWB"
Returns: 6
"WBBWBBBWBWWWBWBWWBBWBWBWBBBBBWBWBWBBBBBBWWBBBBWBWW"
Returns: 10
"BWBWBBWBBBWWWBBWBBWWBBBBBBBWWBBBWBWWBBBBBBBWBBWWBB"
Returns: 10
"BWBWBWBWWBWWBBBBWBBBWBBBBWWWBBBBBBBWBWBWBWWWBBWWWB"
Returns: 12
"BBWWWBBBBWWBWBBWWWWWWBWBWBWBWBWWBBBBBWWWWWWBWWBWWB"
Returns: 13
"BWWWWBWBWBBWBBWBBWWWBWBBBWBWWWWBWWWWWBWBBWWBWBWWBB"
Returns: 14
"WBBBWWWBWWWWBWWBWWBBWBWBBBBWBBWBBWBBBBWWWWWWBBBWWB"
Returns: 11
"WBBBBWWWWBBWWBBWBBWWWWWWWWWWBBWWWBBBBWWWWBWBWWWWWW"
Returns: 12
"WWBBBBBWWBBBWWWWBWBWWWBWWWWWBWBWBWBBBBWWBBWWBWWWWW"
Returns: 12
"WWWBWBBWWWWWBWWBBWWWBBWWWBBBBBWBBWBBWBWBBBWWBBWBBB"
Returns: 8
"BBBWWWWBBBBWBBBBBWWWWBWBWWWBBWWWWWWWBWWWBWBWBWBWWW"
Returns: 16
"BBBBB"
Returns: 0
"BBWWBBWBBBBBWWBBBBWWWWBBBBWBWBWBWWWBBWWWBBBWWWB"
Returns: 13
"BBBBBBBBBBW"
Returns: 1
"WBWBW"
Returns: 1
"BWBBBBBBBBBBBBBB"
Returns: 1
"BBBBW"
Returns: 1
"WWWWWBW"
Returns: 1
"BBWWWWWBBWWWB"
Returns: 3
"WWWWWWWWWWWWWWWBW"
Returns: 1
"WWWWBW"
Returns: 1
"WBWBWBW"
Returns: 2
"BWWBWW"
Returns: 2
"WWWWWWWWWWW"
Returns: 0
"WWWWW"
Returns: 0
"WWWWWWWWWWWWWB"
Returns: 0
"BWBBB"
Returns: 1
"WBBBBB"
Returns: 0
"BBBBWW"
Returns: 2
"WWBBBBBBBB"
Returns: 0
"WWWBW"
Returns: 1
"WWWWWWBW"
Returns: 1
"BWWWW"
Returns: 1
"WBBBBBBBB"
Returns: 0
"BBBBBBBBB"
Returns: 0
"BBBBBBBBBBBB"
Returns: 0
"BBBBBBBBBW"
Returns: 1
"BWBBBBB"
Returns: 1