The trick is how we store the pattern map. The question is to perform search in constant space, so if we store the pattern map as a Trie, then we can you two pointer approach to perform the required task.

The trick is how we store the pattern map. The question is to perform search in constant space, so if we store the pattern map as a Trie, then we can you two pointer approach to perform the required task.