This is essentially the same as substr and a hash, but will run faster. Forgive the funky pseudocode.
sub is_compatible {
my T, B;
foreach i from 0 .. stringlen {
next if top[i] eq bottom[i]
if top[i] is a digit
return NOT COMPATIBLE if bottom[i] is a digit
T |= top[i]
if bottom[i] is a digit
B |= bottom[i]
}
return T&B ? NOT COMPATIBLE : COMPATIBLE
}
Assumptions: your "digits" are weakly fewer in number than your integer1 width. If by digit you really do mean 0 .. 9, I think this obtains on all the machines Perl runs on. In which case you can also optimize "is a digit" with low-level ASCII checks. Also, you should probably only split each string once instead of seeking into it. If this isn't fast enough, this is a good candidate for inlining in c.
1 More precisely, whatever does efficient bitwise arithmetic.