It's NP hard, even if you disallow backreferences. There's a simple mapping from the exact set cover problem to this problem.
If you have sets S[1], S[2], ..., S[n]
(with union S
), and without loss of generality, the sets contain all the numbers 1...N for some N. Represent the S[i]
as a string of length N, with 1
in the k'th place if k is in S[i]
, and 0
otherwise.
Let the columns of your regexp puzzle be all the same -- 0*10*
, and the k'th row be "(S[k])|(0*)".
For example, if S[1] = {1, 4}, S[2] = {2}, S[3] = {3}
, and S[4] = {2, 3}
, then the puzzle would be:
0*10* 0*10* 0*10* 0*10*
1001|0*
0100|0*
0010|0*
0110|0*
A solution to this regexp puzzle is an exact cover of {1, 2, 3, 4} with the S[i]
.