2
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
@Don Thousand please stop rewriting x to X. – kyrill – 2019-12-10T00:51:59.780
Note that in your own question, $X$ refers to the master sequence. – Don Thousand – 2019-12-10T01:44:44.067
Could a better measure be O(m + alphabetSize)? – Jonathan Allan – 2019-12-10T01:45:04.470
Some test cases:
"aaabbb", "ab"
and"aaabbb", "aabb"
should both return9
, maybe add some for0
and1
mappings. – Matthew Jensen – 2019-12-10T02:15:23.983@DonThousand note that in my question I state $X = x_1\dots x_n$. – kyrill – 2019-12-10T02:20:36.903
@JonathanAllan I don't know. If you think it's better then maybe it is; I haven't really thought about it thoroughly. – kyrill – 2019-12-10T02:22:43.260
Done, remove the last one if you think it's unnecessary. – Matthew Jensen – 2019-12-10T02:29:45.137