2020-12-29

Find a prefix based on trie search | | python

Problem : Given a list of business_names (strings) and a searchTerm (string). Return a list of business_names that contains searchTerm as prefix in the business_names.

Example 1.
Input:

business_names[] = { "burger king", "McDonald's", "super duper burger's", "subway", "pizza hut"}
searchTerm = "bur"

Ouput:
["burger king", "super duper burger's"]

I have tried solving in this below way.

But I want to implement trie approach to solve this problem. some one please help here? https://www.geeksforgeeks.org/trie-insert-and-search/

Any linear solution to solve

def prefix(business_names, searchTerm):
    split = [i.split() for i in business_names]
    ans = []
    for name in split:
        for i in range(len(name)):
            query = ' '.join(name[i:])
            if query.startswith(searchTerm):
                ans.append(name)
                break
    return [' '.join(i) for i in ans]


from Recent Questions - Stack Overflow https://ift.tt/2KLa4OT
https://ift.tt/eA8V8J

No comments:

Post a Comment