-
Notifications
You must be signed in to change notification settings - Fork 0
/
259.py
53 lines (40 loc) · 1.44 KB
/
259.py
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
"""
Problem:
Ghost is a two-person word game where players alternate appending letters to a word.
The first person who spells out a word, or creates a prefix for which there is no
possible continuation, loses. Here is a sample game:
Player 1: g
Player 2: h
Player 1: o
Player 2: s
Player 1: t [loses]
Given a dictionary of words, determine the letters the first player should start with,
such that with optimal play they cannot lose.
For example, if the dictionary is ["cat", "calf", "dog", "bear"], the only winning
start letter would be b.
"""
from typing import List, Set
def get_winning_letters(words: List[str]) -> Set[str]:
# requirements for winning start letter:
# - the length is even for all words starting with the character
starting_char_freq = {}
for word in words:
if word[0] not in starting_char_freq:
starting_char_freq[word[0]] = []
starting_char_freq[word[0]].append(word)
winning_start_letters = set()
for starting_char in starting_char_freq:
for word in starting_char_freq[starting_char]:
if len(word) % 2 != 0:
break
else:
winning_start_letters.add(starting_char)
return winning_start_letters
if __name__ == "__main__":
print(get_winning_letters(["cat", "calf", "dog", "bear"]))
print(get_winning_letters(["something", "hi", "cat", "dog", "bear", "hola"]))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""