In how many ways is it possible to re-arrange the sequence $(1,1,2,2,3,3,4,4)$, such that $(1,2,3,4)$ is a subsequence of it?
In case the question is not clear: what is the number of functions $f\negthinspace : [8]\to [4]$, such that for all $j \in [4]$ there exist exactly two elements $i_1,i_2\in [8]$ s.t. $f(i_1)=f(i_2)=j$, and s.t. there exists $i_1<i_2<i_3<i_4$ in $[8]$ with $f(i_1)<f(i_2)<f(i_3)<f(i_4)$?
I have tried to solve this using the inclusion-exclusion principle, but I couldn't really find the appropriate sets to take. I also tried to give it a recursive formula, but again failed.
I brute-force calculated the final result to be $641$.
EDIT: the code I used is
import numpy as np
from collections import Counter
def has_subsequence(sequence, subseq, start_index=0):
if len(subseq) == 1:
return sequence.find(subseq[0], start_index) >= 0
if len(sequence) <= len(subseq):
return sequence == subseq
char_index = sequence.find(subseq[0], start_index)
if char_index == -1:
return False
return has_subsequence(sequence, subseq[1:], char_index + 1)
def count_perms_of_00112233_with_substring_0123():
count = 0
for sequence in (np.base_repr(i, base=4).zfill(8) for i in range(4**8 + 1)):
if all(count_i == 2 for count_i in Counter(sequence).values()):
if has_subsequence(sequence, "0123"):
count += 1
print("Number of valid sequences:", count)
Here is an inclusion-exclusion approach: $$\sum_{k=0}^4 (-1)^k\binom{4}{k}\binom{8}{k+4}(4-k)! = 641$$ The idea is to choose $k$ of the elements among $\{1,2,3,4\}$ to appear twice (and the other $4-k$ once), choose $k+4$ of the $8$ positions to place these elements in ascending order, and then permute the remaining $8-(k+4)=4-k$ (distinct) elements arbitrarily.
Here's a bit more explanation. A naive count of permutations of $(1,1,2,2,3,3,4,4)$ that contain $(1,2,3,4)$ as a subsequence would choose the $4$ positions in $\binom{8}{4}$ ways and then permute the remaining $4$ elements in $4!$ ways, yielding $\binom{8}{4} 4! = 70\cdot 24 = 1680$. Pictorially, we have $$\_1\_2\_3\_4\_,$$ where each $\_$ corresponds to a nonnegative number of elements.
But this overcounts when $(1,2,3,4)$ appears more than once. To correct for this overcount, consider $4$ cases: $$ \_1\_1\_2\_3\_4\_ \\ \_1\_2\_2\_3\_4\_ \\ \_1\_2\_3\_3\_4\_ \\ \_1\_2\_3\_4\_4\_ $$ In each case, choose the $5$ positions in $\binom{8}{5}$ ways and then permute the remaining $3$ elements in $3!$ ways, yielding $4\binom{8}{5} 3! = 4\cdot 56\cdot 6 = 1344$. So a naive correction to the original overcount would yield $1680-1344=336$.
But we have overcorrected when $(1,2,3,4)$ appears more than twice. So add back in the counts for the following $\binom{4}{2}=6$ cases: $$ \_1\_1\_2\_2\_3\_4\_ \\ \_1\_1\_2\_3\_3\_4\_ \\ \_1\_1\_2\_3\_4\_4\_ \\ \_1\_2\_2\_3\_3\_4\_ \\ \_1\_2\_2\_3\_4\_4\_ \\ \_1\_2\_3\_3\_4\_4\_ $$ In each case, choose the $6$ positions in $\binom{8}{6}$ ways and then permute the remaining $2$ elements in $2!$ ways, yielding $\binom{4}{2}\binom{8}{6} 2! = 6\cdot 28\cdot 2 = 336$. Our count so far is $1680-1344+336=672$.
But we have overcorrected in the following $\binom{4}{3}=4$ cases: $$ \_1\_1\_2\_2\_3\_3\_4\_ \\ \_1\_1\_2\_2\_3\_4\_4\_ \\ \_1\_1\_2\_3\_3\_4\_4\_ \\ \_1\_2\_2\_3\_3\_4\_4\_ $$ In each case, choose the $7$ positions in $\binom{8}{7}$ ways and then permute the remaining $1$ element in $1!$ way, yielding $\binom{4}{3}\binom{8}{7} 1! = 4\cdot 8\cdot 1 = 32$. Our count so far is $1680-1344+336-32=640$.
Finally, add back $\binom{4}{4}\binom{8}{8} 0!=1$ for $11223344$, yielding $1680-1344+336-32+1=641$.