-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAY168 - Complete String
More file actions
53 lines (51 loc) · 1.56 KB
/
DAY168 - Complete String
File metadata and controls
53 lines (51 loc) · 1.56 KB
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
https://www.codingninjas.com/codestudio/problems/complete-string_2687860?utm_source=youtube&utm_medium=affiliate&utm_campaign=striver_tries_videos&leftPanelTab=0
***************************************
from os import *
from sys import *
from collections import *
from math import *
from typing import *
class TrieNode:
def __init__(self):
self.children = [None]*26
self.flag = False
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, word):
newRoot = self.root
for ch in word:
alphabetIndex = ord(ch)-ord('a')
if newRoot.children[alphabetIndex] == None:
newRoot.children[alphabetIndex] = TrieNode()
newRoot = newRoot.children[alphabetIndex]
newRoot.flag = True
def search(self, word):
newRoot = self.root
for ch in word:
alphabetIndex = ord(ch) - ord('a')
newRoot = newRoot.children[alphabetIndex]
if newRoot.flag == False:
return False
return True
def completeString(n: int, a: List[str])-> str:
trie = Trie()
for word in a:
trie.insert(word)
maxString = ""
maxSlen = 0
for word in a:
x = trie.search(word)
# print(word,x)
if not x:
continue
if len(word) == maxSlen:
if word<maxString:
maxString = word
if len(word)>maxSlen:
maxString = word
maxSlen = len(word)
if len(maxString) == 0:
return None
return maxString
pass