Problem Statement
You are given a String cards, where each character represents the color of a card. You then select two of those cards at random. Return the probability that they are the same color.
Definition
- Class:
- PickTwoCards
- Method:
- equalProbability
- Parameters:
- String
- Returns:
- double
- Method signature:
- double equalProbability(String cards)
- (be sure your method is public)
Notes
- A return value that is less than 1e-9 absolute or relative error is considered correct.
Constraints
- cards will contain between 2 and 50 characters, inclusive.
- Each character of cards will be between 'A' and 'Z', inclusive.
Examples
"AAB"
Returns: 0.3333333333333333
There are three ways to pick two cards, one of which is both "A" cards."ABC"
Returns: 0.0
Can't get two the same when there's only one of each."AA"
Returns: 1.0
Only two cards to choose, so we always get a matched pair."ABABA"
Returns: 0.4