Problem solution · Python

Prefix and Suffix Search

Prefix and Suffix Search: a Python solution using hash-based lookup. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Hash-based lookup
Source
Kamyu LeetCode Solutions
Length
103 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Hash-based lookup

For Prefix and Suffix Search, the implementation stores previously seen values or frequencies in a hash table for direct membership and lookup operations.

  1. Decide the key that represents the information needed later.
  2. Update its count or stored state while scanning the input.
  3. Use constant-time expected lookups to detect matches or assemble the result.

Code notes

  • 103 lines of Python from the credited upstream file prefix-and-suffix-search.py.
  • The implementation visibly relies on sequence storage, hash lookup.
  • No explicit loop blocks detected.

Complexity

Expected hash operations are constant time, but the surrounding scan and the number of stored keys determine total work and memory.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codePrefix and Suffix Search · PythonPython
Use this to learn the idea, then write your own version.
# Time:  ctor:   O(w * l^2), w is the number of words, l is the word length on average#        search: O(p + s)  , p is the length of the prefix, s is the length of the suffix,# Space: O(t), t is the number of trie nodes import collections  class WordFilter(object):     def __init__(self, words):        """        :type words: List[str]        """        _trie = lambda: collections.defaultdict(_trie)        self.__trie = _trie()         for weight, word in enumerate(words):            word += '#'            for i in xrange(len(word)):                cur = self.__trie                cur["_weight"] = weight                for j in xrange(i, 2*len(word)-1):                    cur = cur[word[j%len(word)]]                    cur["_weight"] = weight     def f(self, prefix, suffix):        """        :type prefix: str        :type suffix: str        :rtype: int        """        cur = self.__trie        for letter in suffix + '#' + prefix:            if letter not in cur:                return -1            cur = cur[letter]        return cur["_weight"]  # Time:  ctor:   O(w * l), w is the number of words, l is the word length on average#        search: O(p + s + max(m, n)), p is the length of the prefix, s is the length of the suffix,#                                      m is the number of the prefix match, n is the number of the suffix match# Space: O(w * l)class Trie(object):     def __init__(self):        _trie = lambda: collections.defaultdict(_trie)        self.__trie = _trie()     def insert(self, word, i):        def add_word(cur, i):            if "_words" not in cur:                cur["_words"] = []            cur["_words"].append(i)         cur = self.__trie        add_word(cur, i)        for c in word:            cur = cur[c]            add_word(cur, i)     def find(self, word):        cur = self.__trie        for c in word:            if c not in cur:                return []            cur = cur[c]        return cur["_words"]  class WordFilter2(object):     def __init__(self, words):        """        :type words: List[str]        """        self.__prefix_trie = Trie()        self.__suffix_trie = Trie()        for i in reversed(xrange(len(words))):            self.__prefix_trie.insert(words[i], i)            self.__suffix_trie.insert(words[i][::-1], i)     def f(self, prefix, suffix):        """        :type prefix: str        :type suffix: str        :rtype: int        """        prefix_match = self.__prefix_trie.find(prefix)        suffix_match = self.__suffix_trie.find(suffix[::-1])        i, j = 0, 0        while i != len(prefix_match) and j != len(suffix_match):            if prefix_match[i] == suffix_match[j]:                return prefix_match[i]            elif prefix_match[i] > suffix_match[j]:                i += 1            else:                j += 1        return -1    

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗