-
Notifications
You must be signed in to change notification settings - Fork 15
/
alien-rhyme.py
37 lines (32 loc) · 1.06 KB
/
alien-rhyme.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
# Copyright (c) 2019 kamyu. All rights reserved.
#
# Google Code Jam 2019 Round 1A - Problem C. Alien Rhyme
# https://codingcompetitions.withgoogle.com/codejam/round/0000000000051635/0000000000104e05
#
# Time: O(T), t is size of trie, may be up to O(N * l), l is the average length of words
# Space: O(T)
#
import collections
import functools
def alien_rhyme_helper(node):
no_rhyme_count = sum(alien_rhyme_helper(child) for child in node.itervalues() if child)
if "_end" in node:
no_rhyme_count += 1
if "_root" not in node and no_rhyme_count >= 2:
no_rhyme_count -= 2
return no_rhyme_count
def alien_rhyme():
N = input()
Ws = []
for _ in xrange(N):
W = list(raw_input())
W.reverse()
Ws.append(W)
_trie = lambda: collections.defaultdict(_trie)
trie = _trie()
trie.setdefault("_root")
for W in Ws:
functools.reduce(dict.__getitem__, W, trie).setdefault("_end")
return N - alien_rhyme_helper(trie)
for case in xrange(input()):
print 'Case #%d: %s' % (case+1, alien_rhyme())