forked from keshavnandan/Topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHappyLetterDiv2.txt
66 lines (41 loc) · 1.98 KB
/
HappyLetterDiv2.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
PROBLEM STATEMENT
The Happy Letter game is played as follows:
At the beginning, several players enter the field.
Each player has a lowercase English letter on their back.
The game is played in turns.
In each turn, you select two players with different letters, and both selected players leave the field.
The game ends once it is impossible to take another turn.
If there are some players left in the field at the end of the game, they must all have the same letter.
That letter is called the winning letter.
If there are no players left in the field at the end of the game, there is no winning letter.
In general, there may be many different ways how to play the game, and these may lead to different outcomes of the game.
However, sometimes we can look at the initial players and determine that each possible sequence of turns leads to the same winning letter.
That winning letter is then called a happy letter.
You are given a string letters.
The characters in letters are the characters carried by the players at the beginning of the game.
Return a string with one character: either the happy letter (if it exists), or a period ('.') if there is no happy letter.
DEFINITION
Class:HappyLetterDiv2
Method:getHappyLetter
Parameters:string
Returns:char
Method signature:char getHappyLetter(string letters)
CONSTRAINTS
-letters will contain between 1 and 50 elements.
-Each element of letters will be a lowercase English letter ('a'-'z').
EXAMPLES
0)
"aacaaa"
Returns: 'a'
In the first turn we send away the player with the 'c' and one of the players with an 'a'. All remaining players have an 'a', hence the game ends and 'a' is the winning letter.
1)
"dcdjx"
Returns: '.'
Each of the letters 'c', 'd', 'j', and 'x' can be the winning letter. Thus there is no happy letter.
2)
"bcbbbbba"
Returns: 'b'
3)
"aabc"
Returns: '.'
Here, only 'a' can be the winning letter. However, it is also possible that there is no winning letter at the end of the game. Therefore 'a' is not considered to be the happy letter.