Let me know if this task has already been posed. I haven't found it when I looked.
Input
- master sequence \$\ X = x_1\dots x_n\$: sequence of characters, eg. \$\rm international\$
- subsequence \$\ Y = y_1\dots y_m\$: sequence of characters, eg. \$\rm intl\$
Output
Number of possible mappings \$\mu: [1\dots m] \rightarrow [1\dots n]\$ of positions in subsequence to positions in the master sequence, such that for all \$j\in[1\ldots m]\$:
\$y_j = x_{\mu(j)}\$, viz. an occurrence of a character in the subsequence maps to an occurrence of the same character in the master sequence
\$\forall\ i \lt j:\ \mu(i) \lt \mu(j)\$, viz. the mapping preserves mutual position of character occurrences.
In the example above, there are 3 possible mappings:
- \$\rm\underline{int}ernationa\underline{l}\$
- \$\rm\underline{in}terna\underline{t}iona\underline{l}\$
- \$\rm\underline{i}nter\underline{n}a\underline{t}iona\underline{l}\$
Rules
Lowest average asymptotic time complexity with respect to \$n+m\$ wins.
If it helps you, you can assume the input is a sequence of bytes (values 0-255).
Test cases
master sequence, subsequence -> number of possible mappings
international, intl -> 3
aaabbb, ab -> 9
aaabbb, aabb -> 9
only one, onl -> 1
none, en -> 0
"aaabbb", "ab"
and"aaabbb", "aabb"
should both return9
, maybe add some for0
and1
mappings. \$\endgroup\$