Problem solution · Python

Stream of Characters2

Stream of Characters2: a Python solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Breadth-first search
Source
Kamyu LeetCode Solutions
Length
83 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

Breadth-first search

For Stream of Characters2, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 83 lines of Python from the credited upstream file stream-of-characters2.py.
  • The implementation visibly relies on sequence storage, hash lookup, work queue.
  • No explicit loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

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 codeStream of Characters2 · PythonPython
Use this to learn the idea, then write your own version.
# Time:  ctor:  O(n + p^2), n is the total size of patterns#                         , p is the number of patterns#        query: O(m + z), m is the total size of query string#                       , z is the number of all matched strings #                       , query time would be O(m) if we don't use all the matched patterns# Space: O(t + p^2), t is the total size of ac automata trie#                  , space could be further improved by DAT (double-array trie) # Aho–Corasick automata# reference:# - http://web.stanford.edu/class/archive/cs/cs166/cs166.1166/lectures/02/Small02.pdf# - http://algo.pw/algo/64/python import collections  class AhoNode(object):    def __init__(self):        self.children = collections.defaultdict(AhoNode)        self.suffix = None        self.outputs = []  class AhoTrie(object):     def step(self, letter):        while self.__node and letter not in self.__node.children:            self.__node = self.__node.suffix        self.__node = self.__node.children[letter] if self.__node else self.__root        return self.__node.outputs  # Time:  O(z), it would be O(m) if we don't use all the matched patterns        def __init__(self, patterns):        self.__root = self.__create_ac_trie(patterns)        self.__node = self.__create_ac_suffix_and_output_links(self.__root)        def __create_ac_trie(self, patterns):  # Time:  O(n), Space: O(t)        root = AhoNode()        for i, pattern in enumerate(patterns):            node = root            for c in pattern:                node = node.children[c]            node.outputs.append(i)        return root     def __create_ac_suffix_and_output_links(self, root):  # Time:  O(n + p^2), Space: O(t + p^2)        queue = collections.deque()        for node in root.children.itervalues():            queue.append(node)            node.suffix = root         while queue:            node = queue.popleft()            for c, child in node.children.iteritems():                queue.append(child)                suffix = node.suffix                while suffix and c not in suffix.children:                    suffix = suffix.suffix                child.suffix = suffix.children[c] if suffix else root                child.outputs += child.suffix.outputs  # Time: O(p^2)                return root  class StreamChecker(object):     def __init__(self, words):        """        :type words: List[str]        """        self.__trie = AhoTrie(words)     def query(self, letter):  # O(m) times        """        :type letter: str        :rtype: bool        """        return len(self.__trie.step(letter)) > 0         # Your StreamChecker object will be instantiated and called as such:# obj = StreamChecker(words)# param_1 = obj.query(letter) 

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 ↗