L1
and L2
are two lists which only consist of X
's and Y
's. L1
and L2
have the same length.
How many sublists of L1
contain the same number of X
's as the sublist of L2
in the same position? The empty sublist is not counted.
For example:
L1
=X
L2
=Y
=> return0
L1
=Y, Y
L2
=Y, Y
=> return3
L1
=Y, X, Y
L2
=X, Y, X
=> return2
L1
=Y, Y, X, X
L2
=X, X, Y, Y
=> return2
A brute-force approach would have time complexity O(n^2)
. I think (but I am not sure) there's an O(n)
solution, but I can't put my finger on it.