Trie | (Insert and Search)
Trie is an efficient information retrieval data structure. Use this data structure to store Strings and search strings. Your task is to use TRIE data structure and search the given string A. If found print 1 else 0.
Example 1:
Input: N = 8 key[] = {the,a,there,answer,any,by, bye,their} search = the Output: 1 Explanation: the is present in the given string "the a there answer any by bye their"
Example 2:
Input: N = 8 key[] = {the,a,there,answer,any,by, bye,their} search = geeks Output: 0 Explanation: geeks is not present in the given string "the a there answer any by bye their"
Your Task:
Complete insert and search function and return true if key is present in the formed trie else false in the search function. (In case of true, 1 is printed and false, 0 is printed by the driver's code.
Expected Time Complexity: O(M+|search|).
Expected Auxiliary Space: O(M).
M = sum of the length of all strings which is present in the key[]
|search| denotes the length of the string search.
Answer:
for i in range(t):
n= int(input())
s= input()
s.split()
f= input()
l= s.split()
flag=0
for k in l:
if(k==f):
flag=1
break
print(flag)
for i in range(n):
no=int(input())
inp=input().split()
inp1=input()
if inp1 in inp:
print(1)
else:
print(0)
Comments
Post a Comment