-
Notifications
You must be signed in to change notification settings - Fork 0
/
NoPrefixSet.py
58 lines (47 loc) · 1.2 KB
/
NoPrefixSet.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
54
55
56
57
58
# https://www.hackerrank.com/challenges/no-prefix-set/problem
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'noPrefix' function below.
#
# The function accepts STRING_ARRAY words as parameter.
#
class TrieNode:
def __init__(self):
self.children = {}
self.is_end_of_word = False
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, word):
current = self.root
for char in word:
if char not in current.children:
current.children[char] = TrieNode()
current = current.children[char]
if current.is_end_of_word:
return False
if current.children:
return False
current.is_end_of_word = True
return True
def noPrefix(words):
# Write your code here
trie = Trie()
for word in words:
if not trie.insert(word):
print("BAD SET")
print(word)
return
print("GOOD SET")
if __name__ == '__main__':
n = int(input().strip())
words = []
for _ in range(n):
words_item = input()
words.append(words_item)
noPrefix(words)