首页 > 其他 > 详细

Suffix Trie Construction

时间:2021-05-29 09:23:12      阅读:17      评论:0      收藏:0      [点我收藏+]

refer to: https://www.algoexpert.io/questions/Suffix%20Trie%20Construction

Problem statement

技术分享图片

 

 Example

技术分享图片

 

 Analysis

step 1

技术分享图片

 

 step 2

技术分享图片

 

 step 3

技术分享图片

 

 Time/ Space Complexity

技术分享图片

 

 Code

class SuffixTrie:
    def __init__(self, string):
        self.root = {}
        self.endSymbol = "*"
        self.populateSuffixTrieFrom(string)

    # creation function
    def populateSuffixTrieFrom(self, string):
        for i in range(len(string)):
            self.insertSubstringStartingAt(i, string)

    def insertSubstringStartingAt(self, i, string):
        node = self.root
        for j in range(i, len(string)):
            letter = string[j]
            if letter not in node:
                node[letter] = {} # create an empty hashmap
            node = node[letter] # update the currect node
        node[self.endSymbol] = True # after iterate one whole string, add * at the end
        
    #search function
    def contains(self, string):
        node = self.root
        for letter in string:
            if letter not in node:
                return False
            node = node[letter]# if letter in node, keep down traversing the suffix tree
        return self.endSymbol in node # avoid some cases that a string has not been traversed totally 
        
        

 

 

Suffix Trie Construction

原文:https://www.cnblogs.com/LilyLiya/p/14824375.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!