Open In App

Prefix matching in Python using pytrie module

Last Updated : 21 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given a list of strings and a prefix value sub-string, find all strings from given list of strings which contains given value as prefix ? Examples:

Input : arr = ['geeksforgeeks', 'forgeeks', 
               'geeks', 'eeksfor'], 
       prefix = 'geek'
Output : ['geeksforgeeks','geeks']

A Simple approach to solve this problem is to traverse through complete list and match given prefix with each string one by one, print all strings which contains given value as prefix. We have existing solution to solve this problem using Trie Data Structure. We can implement Trie in python using pytrie.StringTrie() module.

Create, insert, search and delete in pytrie.StringTrie() ?

  • Create : trie=pytrie.StringTrie() creates a empty trie data structure.
  • Insert : trie[key]=value, key is the data we want to insert in trie and value is similar to bucket which gets appended just after the last node of inserted key and this bucket contains the actual value of key inserted.
  • Search : trie.values(prefix), returns list of all keys which contains given prefix.
  • Delete : del trie[key], removes specified key from trie data structure.

Note : To install pytrie package use this pip install pytrie –user command from terminal in linux

Python3




# Function which returns all strings
# that contains given prefix
from pytrie import StringTrie
 
def prefixSearch(arr,prefix):
     
    # create empty trie
    trie=StringTrie()
 
    # traverse through list of strings
    # to insert it in trie. Here value of
    # key is itself key because at last
    # we need to return
    for key in arr:
        trie[key] = key
 
    # values(search) method returns list
    # of values of keys which contains
    # search pattern as prefix
    return trie.values(prefix)
 
# Driver program
if __name__ == "__main__":
    arr = ['geeksforgeeks','forgeeks','geeks','eeksfor']
    prefix = 'geek'
    output = prefixSearch(arr,prefix)
    if len(output) > 0:
        print (output)
    else:
        print ('Pattern not found')


Output:

['geeksforgeeks','geeks']


Similar Reads

Python Program To Find Longest Common Prefix Using Word By Word Matching
Given a set of strings, find the longest common prefix. Examples: Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”} Output : "gee" Input : {"apple", "ape", "april"} Output : "ap"Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. We start with an example. Suppose there are two strings- “geeksforgeeks” and “geeks”
4 min read
Java Program To Find Longest Common Prefix Using Word By Word Matching
Given a set of strings, find the longest common prefix. Examples: Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”} Output : "gee" Input : {"apple", "ape", "april"} Output : "ap"Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. We start with an example. Suppose there are two strings- “geeksforgeeks” and “geeks”
5 min read
Javascript Program To Find Longest Common Prefix Using Word By Word Matching
Given a set of strings, find the longest common prefix. Examples: Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”} Output : "gee" Input : {"apple", "ape", "april"} Output : "ap" Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. We start with an example. Suppose there are two strings- “geeksforgeeks” and “geeks
3 min read
C++ Program To Find Longest Common Prefix Using Word By Word Matching
Given a set of strings, find the longest common prefix. Examples: Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”} Output : "gee" Input : {"apple", "ape", "april"} Output : "ap" Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. We start with an example. Suppose there are two strings- “geeksforgeeks” and “geeks
3 min read
Longest Common Prefix using Word by Word Matching
Given a set of strings, find the longest common prefix. Examples: Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”} Output : "gee" Input : {"apple", "ape", "april"} Output : "ap"Recommended PracticeLongest Common Prefix in an ArrayTry It! We start with an example. Suppose there are two strings- “geeksforgeeks” and “geeks”. What is the longest co
15 min read
Longest Common Prefix using Character by Character Matching
Given a set of strings, find the longest common prefix. Input : {“geeksforgeeks”, “geeks”, “geek”, “geezer”} Output : "gee" Input : {"apple", "ape", "april"} Output : "ap"Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. We have discussed word by word matching algorithm in previous post.In this algorithm, instead
13 min read
Longest Common Prefix Matching | Set-6
Given a set of strings, find the longest common prefix. Examples: Input: str[] = {geeksforgeeks, geeks, geek, geezer} Output: gee Input: str[] = {apple, ape, april} Output: ap Previous Approaches: Set1 | Set2 | Set3 | Set4 | Set5 Approach: Sort the given set of N strings.Compare the first and last string in the sorted array of strings.The string wi
6 min read
Longest Prefix Subsequence matching Fibonacci Sequence
Given an array arr of size N. Also, there is another array B of the infinite size where B[0] = B[1] = 1, and for all (i >= 2), B[i] = B[i-1]+B[i-2], the task is to find the length of the longest subsequence which is the prefix of array B. If there is no subsequence which is the prefix of array B then return 0. Examples: Input: N = 6, arr = {1, 2
5 min read
Longest prefix matching - A Trie based solution in Java
Given a dictionary of words and an input string, find the longest prefix of the string which is also a word in dictionary. Examples: Let the dictionary contains the following words: {are, area, base, cat, cater, children, basement} Below are some input/output examples: -------------------------------------- Input String Output ---------------------
4 min read
Os Module Vs. Sys Module In Python
Python provides a lot of libraries to interact with the development environment. If you need help using the OS and Sys Module in Python, you have landed in the right place. This article covers a detailed explanation of the OS and Sys Module including their comparison. By the end of this article, you will be able to easily decide which module suits
5 min read
three90RightbarBannerImg