Here is one approach that works in polynomial time. This is a bit heavyweight, and there may be a more effective solution.
The first point, which, I think, helps revise the issue here. Instead of asking if these patterns match each other, ask this equivalent question:
Given the given patterns P1 and P2, there exists a string w, where P1 and P2 correspond to w?
In other words, instead of trying to match two patterns to each other, we will look for a string that matches each pattern.
You may have noticed that the types of patterns you can work with are a subset of regular expressions. This is useful because there is a rather complicated theory of what you can do with regular expressions and their properties. Therefore, instead of striving for your original problem, solve it even more general:
Given the two regular expressions R1 and R2, is there a string w that matches both R1 and R2?
The reason for solving this more general problem is that it allows you to use a theory developed around regular expressions. For example, in the formal theory of language we can talk about the language of a regular expression, which is a set of all lines that correspond to a regular expression. Denote this by L (R). If there is a line that matches the two regular expressions R1 and R2, then this line belongs to both L (R1) and L (R2), so our question is equivalent
For two regular expressions R1 and R2, there is a string w in L (R1) & cap; L (R 2)?
So far, all we have done is to reconsider the problem that we want to solve. Now release the solution.
The key point here is the ability to convert any regular expression into an NFA (non-deterministic finite state machine), so that every line matching the regular expression is accepted by the NFA and vice versa. Even better, the resulting NFA can be built in polynomial time. So let's start by building an NFA for each input regular expression.
Now that we have these NFAs, we want to answer this question: is there a line that both NFAs accept? And, fortunately, there is a quick way to answer this question. There is a general NFA construct called the product construct, which, given the two NFAs N1 and N2, creates a new NFA N 'that accepts all strings accepted by both N1 and N2, and no other strings. Again, this design runs in polynomial time.
Once we have N ', we basically finished! All we need to do is run a breadth or depth search in the N 'states to see if we find the receiving state. If so, great! This means that the string received by N 'means that there is a string accepted by both N1 and N2, which means that there is a string matched by both R1 and R2, so the answer to the original question is βyes!β And vice versa, if we cannot reach the receiving state, then the answer is "no, this is impossible."
I am sure that there is a way to do all this implicitly by doing some kind of implicit BFS on the N 'machine, without actually creating it, and this should be possible to do in something like O (n 2 ) time. If I have some more time, I will go on to this answer and talk about how to do it.