Given two strings s and t, find the minimum number of times s must be appended to itself so that t becomes a subsequence of the resulting string.
In other words, return the smallest integer k such that t is a subsequence of s repeated k times (s + s + ... + s, with k copies of s). If t can never be a subsequence, however many times s is repeated, return -1.
Function Signature
def min_appends(s: str, t: str) -> int:
Rules
-
The answer counts copies of
s
. If
t
is already a subsequence of
s
itself, the answer is
1
.
-
A subsequence keeps the relative order of the characters, and its characters do not have to be adjacent. For example,
"ace"
is a subsequence of
"abcde"
.
Constraints
-
1 <= s.length <= 10^5
-
1 <= t.length <= 10^5
-
s
and
t
consist of lowercase English letters.
Examples
Input: s = "abc", t = "abcbc"
Output: 2
One copy, "abc", holds only the first three characters of t. Two copies, "abcabc", contain "abcbc": a, b and c come from the first copy, and b and c from the second.
Input: s = "ab", t = "d"
Output: -1
The letter d never appears in s, so no number of copies can contain t.
Input: s = "xyz", t = "xzyxz"
Output: 3
In "xyzxyz" the earliest matches are x at index 0, z at 2 and y at 4, and no x remains after index 4, so two copies are not enough. A third copy supplies x at index 6 and z at index 8.