11
Introduction
In this challenge, your task is to find generalized subsequences of strings. The subsequences are not necessarily contiguous, and they can also "wrap around" the string, going past its end and starting again from the beginning. You'll want to minimize the number of wraps, though.
More formally, let u
and v
be any two strings, and k ≥ 0
an integer.
We say that u
is a k
-wrapping subsequence of v
, if there are distinct indices i1, i2, ..., ilen(u)
such that u == v[i1] v[i2] ... v[ilen(u)]
, and at most k
of the indices ij
satisfy ij > ij+1
.
This means that u
can be found inside v
by going from left to right, choosing some of its characters on the way, and wrapping around at most k
times (equivalently, doing at most k+1
sweeps across v
).
Note that no character can be chosen more than once, even after a wrap-around, and that 0
-wrapping subsequences are exactly the ordinary subsequences that we are all familiar with.
The Task
Your inputs are two non-empty alphanumeric strings u
and v
, and your output is the smallest integer k
such that u
is a k
-wrapping subsequence of v
.
If no such k
exists, the output shall be -1
.
Example
Consider the inputs u := xyzyxzzxyx
and v := yxzzazzyxxxyz
.
If we start looking for the characters of u
in v
in a greedy fashion, we will wrap around 3 times:
yxzzazzyxxxyz
>─x─────y────z┐
┌─────────────┘
└y───────x────┐
┌─────────────┘
└──zz─────x─y─┐
┌─────────────┘
└──────────x──>
Thus the correct output is at most 3.
Note how the left-most character x
is selected once, and then ignored on the second sweep, since it cannot be re-used.
However, there exists a shorter method with only 2 wrap-arounds:
yxzzazzyxxxyz
>──────────xyz┐
┌─────────────┘
└yxzz────x────┐
┌─────────────┘
└───────y─x───>
It turns out that one wrap-around (that is, two sweeps) is not enough, so the correct output is 2
.
Rules and Bonuses
You can write either a function or a full program, and you can also change the order of the inputs if needed. The lowest byte count wins, and standard loopholes are disallowed.
There is a bonus of -10% for computing all of the test cases in under 10 seconds total. I will test unclear cases on my machine; my reference implementation in Python takes about 0.6 seconds. I have a 7-year old laptop with 1.86 GHz dual core CPU, which you may want to take into account.
Test Cases
"me" "moe" -> 0
"meet" "metro" -> -1
"ababa" "abaab" -> 1
"abaab" "baabaa" -> 1
"1c1C1C2B" "1111CCCcB2" -> 3
"reverse" "reserved" -> 2
"abcdefg" "gfedcba" -> 6
"xyzyxzzxyx" "yxzzazzyxxxyz" -> 2
"aasdffdaasdf" "asdfddasdfsdaafsds" -> 2
1
Would this also be a valid solution for the example? It's a greedy approach.
– orlp – 2015-04-12T16:59:00.703@orlp It is not valid, because the first
x
is used in three distinct sweeps. It can only be used once. – Zgarb – 2015-04-12T17:01:13.990Ahhh, I see now. – orlp – 2015-04-12T17:15:44.700