Connection Status:
Competition Arena > PickTwoCards
Rookie SRM 5 · 2021-05-12 · by t-mac · Math
Class Name: PickTwoCards
Return Type: double
Method Name: equalProbability
Arg Types: (string)
Problem Statement

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.

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
0)
"AAB"
Returns: 0.3333333333333333

There are three ways to pick two cards, one of which is both "A" cards.

1)
"ABC"
Returns: 0.0

Can't get two the same when there's only one of each.

2)
"AA"
Returns: 1.0

Only two cards to choose, so we always get a matched pair.

3)
"ABABA"
Returns: 0.4
4)
"ABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGABCDEFGH"
Returns: 0.12

Submissions are judged against all 70 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class PickTwoCards with a public method double equalProbability(string cards) · 70 test cases · 2 s / 256 MB per case

Submitting as anonymous