TopcoderARCHIVE
Archive/Problems/PickTwoCards
SRM · Problem 13431

PickTwoCards

Problem statement, definition, constraints, and public examples.

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

  1. "AAB"
    Returns: 0.3333333333333333
    There are three ways to pick two cards, one of which is both "A" cards.
  2. "ABC"
    Returns: 0.0
    Can't get two the same when there's only one of each.
  3. "AA"
    Returns: 1.0
    Only two cards to choose, so we always get a matched pair.
  4. "ABABA"
    Returns: 0.4
← All problems