Problem Statement
You are given a
You are also given a
Assume that the magician chose the swaps he makes uniformly at random. That is, in each turn with probability 50% he swapped the hats on spots 0 and 1, and with probability 50% he swapped the hats on spots 1 and 2. Return the number of the spot that is most likely to contain the ball at the end of the game. If multiple spots are tied for the largest probability, return the smallest one of them.
Definition
- Class:
- BallAndHats
- Method:
- getHat
- Parameters:
- String, int
- Returns:
- int
- Method signature:
- int getHat(String hats, int numSwaps)
- (be sure your method is public)
Notes
- Two hats are adjacent if their spots differ by 1.
Constraints
- hats will contain exactly three characters.
- hats will contain exactly one 'o' character.
- hats will contain exactly two '.' characters.
- numSwaps will be between 0 and 1000, inclusive.
Examples
".o."
1
Returns: 0
The spots 0 and 2 are equally likely to contain the ball after the hat that contains it is swapped once. We return the smallest spot number, which is 0.
"..o"
0
Returns: 2
The ball does not change spots when 0 swaps are performed; therefore, the ball must be at spot 2.
"o.."
1
Returns: 1
"..o"
2
Returns: 0
"o.."
101
Returns: 1
"o.."
1000
Returns: 0
".o."
1000
Returns: 1
"..o"
1000
Returns: 0
"o.."
0
Returns: 0
".o."
0
Returns: 1
"..o"
1
Returns: 1
"o.."
999
Returns: 1
".o."
999
Returns: 0
"..o"
999
Returns: 1
"o.."
2
Returns: 0
".o."
2
Returns: 1
"o.."
17
Returns: 1
".o."
892
Returns: 1
"..o"
727
Returns: 1
"..o"
213
Returns: 1
"o.."
777
Returns: 1
"o.."
776
Returns: 0
".o."
774
Returns: 1
".o."
775
Returns: 0
"..o"
231
Returns: 1
"..o"
232
Returns: 0
".o."
444
Returns: 1
".o."
447
Returns: 0
"o.."
3
Returns: 1
".o."
3
Returns: 0
"..o"
3
Returns: 1
"o.."
6
Returns: 0
"..o"
4
Returns: 0
".o."
942
Returns: 1
".o."
101
Returns: 0
"..o"
8
Returns: 0
"..o"
100
Returns: 0
"o.."
14
Returns: 0
".o."
4
Returns: 1
"..o"
16
Returns: 0
".o."
399
Returns: 0