We need to determine for each character in s, whether it belongs to at least one subsequence that equals t. Equivalent: For each position i in s, does there exist a way to select a subsequence equal to t that includes position i?
This is reminiscent of problems about covering positions by at least one common subsequence occurrence.
We need to check: For each i, there exists an index j (1-indexed) such that we can match t's characters to positions, with the condition that position i is used to match some character t_k, and the rest can be matched before and after appropriately.
So essentially, we need to know if position i can be used in some subsequence matching t. That depends on whether we can match the prefix of t up to some character using positions before i (excluding i if needed) and match the suffix after that character using positions after i.
We can precompute for each position i in s, the longest prefix of t that can be matched using positions up to i (including possibly i). Similarly, the longest suffix of t that can be matched using positions from i to end (including i). Then i can be used to match some character at index k if there exists k such that we can match prefix of length k-1 using positions before i (i.e., the prefix length before i is at least k-1) and suffix of length (|t|-k) using positions after i (i.e., suffix length after i is at least |t|-k). But also need to ensure that s[i] equals t[k] obviously.
But more precisely, for a given i, we want to know if there exists k with s[i] = t[k] and there exists a match of t that uses i for t[k]. This requires that we can match first k-1 chars with indices < i, and last |t|-k chars with indices > i. So if we compute L[i] = maximum prefix length of t that can be formed as a subsequence using positions up to i (including i? typically we define L[i] as the length of longest prefix of t that can be matched using s[1..i]. Similarly, R[i] = longest suffix of t that can be matched using s[i..|s|]. Then i can be used if there exists k such that s[i] = t[k] and L[i-1] >= k-1 and R[i+1] >= |t|-k. Actually careful: For using i to match t[k], we need that we can match prefix of length k-1 strictly before i, and suffix of length |t|-k strictly after i. So we need L_before = longest prefix using indices < i, and R_after = longest suffix using indices > i. So we can precompute prefix array: pre[i] = length of longest prefix of t that can be matched using first i characters of s (i.e., s[1..i]). That includes possibly using the i-th character. So pre[i] is monotonic non-decreasing. Then pre[i-1] gives the prefix length possible using positions before i (strictly). Similarly, compute suffix array: suf[i] = length of longest suffix of t that can be matched using s[i..n] (including i). So suf[i+1] gives the suffix length possible using positions after i (strictly). Actually careful: If we define suf[i] as the length of suffix of t that can be matched using s[i..n] as a subsequence (starting from the end), we might want to compute from right: maintain a pointer from end of t and count how many suffix chars we can match going backwards. Usually we compute an array right[i] = the maximum number of characters from the end of t that can be matched using s[i..n]. That is, we match t from the end. So if we define suf[i] = maximum p such that t[|t|-p+1 .. |t|] is a subsequence of s[i..n]. Then suf[i] is non-increasing as i increases? Let's compute: as we go leftwards, more characters become available, so suf[i] should be >= suf[i+1]? Actually if we start at i, we have more characters if i is smaller (includes later characters). So suf[i] is typically non-decreasing as i decreases. It's easier: compute array f[i] = the length of longest prefix from the right (i.e., suffix) that can be formed from s[i..n]. We can compute it by scanning from right, using a pointer from the end of t. Let's do: cur = |t|, for i = n down to 1: if cur >= 1 and s[i] == t[cur], then cur--; then suf[i] = |t| - cur? Wait we want suf[i] = number of characters matched from the end after processing i..n. But if we do that, after processing i..n, cur is the smallest index (from start) that we haven't matched? Actually typical: compute from right: maintain an index ptr starting at |t|, iterate i from n downto 1: if ptr >= 1 and s[i] == t[ptr], then ptr--. After processing i, the number of matched characters from the end is |t|-ptr. So we can set suf[i] = |t|-ptr after processing i. But careful: This computes the length of suffix of t that can be matched using the suffix s[i..n] (including i). However, note that as we move leftwards, we might match more characters. So suf[i] will be non-decreasing as i decreases (i.e., larger for smaller i). At i=n, we process only last character, then ptr might decrement if matches last char, so suf[n] = maybe 1 if matches, else 0. Then at i=n-1, we have more characters, so could match more. So yes, suf[i] is the maximum length suffix that can be matched using s[i..n].
Then for position i, the number of suffix characters we can match using positions strictly after i is suf[i+1] (if i+1 <= n). So we need suf[i+1] >= |t|-k.
Alternatively, we could compute arrays leftmost and rightmost matching positions: For each prefix length p, find the earliest index in s that can achieve that prefix. Or for each position i, we can compute the next index to match a given character. But the above approach is O(n) after precomputations, if we test each i? n up to 2e5, that is fine. For each i, we need to see if there exists k such that s[i]=t[k] and pre[i-1] >= k-1 and suf[i+1] >= |t|-k. That could be O(n * |t|) if we try all k. But we can perhaps compute for each character s[i], the range of k that is possible based on prefix and suffix constraints. But note: pre[i-1] and suf[i+1] are numbers. For a given i, we need some k satisfying:
1 <= k <= |t|
s[i] = t[k]
L = pre[i-1] >= k-1 => k <= L+1
R = suf[i+1] >= |t|-k => k >= |t|-R + 1? Let's solve: suf[i+1] >= |t|-k => k >= |t| - suf[i+1].
Actually: suf[i+1] = number of suffix characters matched. That is, we can match the last p characters (p = suf[i+1]) of t. Then we need to be able to match the last |t|-k characters. So condition: |t|-k <= suf[i+1] => k >= |t| - suf[i+1].
So combined constraints:
max(1, |t|-suf[i+1]) <= k <= min(|t|, L+1)
and also s[i] = t[k].
Thus we need to check whether there exists an index k in that interval such that t[k] equals s[i].
So we can precompute for each position i the allowed k range. Then we need to know if any of the t positions in that interval have the character s[i]. That can be done by having for each character, a sorted list of indices in t where that character appears. Then for a given character c = s[i], we can binary search in its list to see if there is any index in the allowed interval.
Total O(n log m) where m is number of occurrences of that character in t, which in worst-case could be O(n log n) but acceptable since n <= 2e5. Actually total O(n log |t|) if each binary search on list of occurrences of that char. Since total occurrences across all chars sum to |t|, the total binary searches is n, each O(log freq) <= O(log |t|). So O(n log |t|) which is fine.
But we can also try to compute something more efficiently: perhaps we can compute for each position i, the "earliest possible k" or "latest possible k" that could be assigned based on greedy matching? There is a known concept: each position i is covered if the leftmost occurrence index (the earliest possible match) for t using i is something. Another approach: For each i, we can compute the leftmost index in t that can be matched using i as part of a full subsequence? Actually we could simulate: For each i, we want to see if there is a full match. This can be done by computing two arrays: preMatch[i] = the position in t matched up to i (like the length). But we also might compute left[i] = the index in t that s[i] would match if we are matching greedily from left? Not exactly.
Given the problem constraints, the binary search approach is simple and efficient.
But we must also ensure that there is at least one subsequence equal to t at all. If there is no subsequence equal to t, then obviously the answer is No, because then there are zero described subsequences, so no character occurs in any. But wait: The condition says "Let's consider all subsequences of string s, coinciding with string t. Is it true that each character of string s occurs in at least one of these subsequences?" If there are no such subsequences, then the set is empty. In that case, it is vacuously false that each character occurs in at least one (since none occur in any). So answer should be No. Indeed, example 3 shows: s=abc, t=ba, there is no occurrence, output No. So we need to check that first. Actually we can incorporate: if no full subsequence, answer directly No. But also our per-character check will likely fail for all i because the condition pre[i-1] >= k-1 and suf[i+1] >= |t|-k for some k will not be possible if overall there is no full match? Possibly some positions could satisfy those constraints even if no full match? Let's think: Suppose there is no full subsequence, meaning that overall the maximum matched prefix length is less than |t|, or the combination fails. However, for a given i, it might still be possible that pre[i-1] + 1 + suf[i+1] >= |t|? Actually we need existence of k such that prefix before i covers up to k-1 and suffix after i covers from k+1 to end. That implies that pre[i-1] >= k-1 and suf[i+1] >= |t|-k. Then sum: (k-1) + 1 + (|t|-k) = |t|, so total coverage = |t|. But pre[i-1] and suf[i+1] are independently computed. It's possible that for some i, these conditions hold even though a full subsequence does not exist globally? Actually if for some i, these conditions hold, then by concatenating the prefix match (positions before i) with i and then suffix match (positions after i), we would get a full subsequence matching t. Because we have a way to match first k-1 chars using positions before i, we use i for char k, and then we can match last |t|-k chars using positions after i. The prefix and suffix matches are independent and their indices are respectively < i and > i, so combining yields a valid subsequence. So if such i exists, then there exists a full subsequence. Conversely, if there is a full subsequence, then at least the positions used in that subsequence satisfy the condition. So existence of any i that satisfies condition implies existence of full subsequence. Thus checking full subsequence existence is redundant if we do per-character check: if any character can be covered, then full subsequence exists. But we need to answer "Yes" only if every character is covered. So if no full subsequence, answer is "No" (since not all characters are covered, indeed none are). So we can just do per-character check; if any character fails, output No. But if all characters pass, that implies there is at least one full subsequence, so fine.
But careful: Could it happen that some characters pass even though no full subsequence exists? As argued, if a character passes, then we can construct a full subsequence. So if there is no full subsequence, no character will pass. So per-character check automatically yields No overall. So we don't need a separate early check.
Thus algorithm:
-
Read s and t.
-
n = len(s), m = len(t).
-
Compute pre array of length n: pre[i] = length of longest prefix of t that can be matched using s[0..i] (0-indexed). We'll compute as we iterate i from 0 to n-1. Initialize ptr = 0. For each i, if ptr < m and s[i] == t[ptr], then ptr++. Set pre[i] = ptr. So after scanning, pre[i] is the prefix length matched up to i inclusive.
Actually note: pre[i] should represent the maximum prefix length achievable using s up to i. The greedy matching from left (taking whenever possible) yields the maximum possible prefix length because we want to match as many as possible early. However, is greedy always maximal? For subsequence matching a given target string, the longest prefix you can achieve up to a point is indeed the greedy count: as you scan, you match t sequentially; because you want to match as many as possible, you take the earliest possible matches. This yields the maximum prefix length because any alternative would not match more characters earlier. So yes, pre[i] is correct.
But careful: pre[i] is the maximum prefix length possible using first i+1 characters. However, the greedy algorithm gives exactly that maximum because it always matches when possible. Formally, it's the length of longest prefix of t that is a subsequence of s[0..i]. The standard technique: iterate s, keep a pointer to next needed char; increment when matched; the pointer value after processing s[0..i] is exactly the length of the longest prefix matched. So pre[i] = pointer.
-
Compute suf array of length n: suf[i] = length of longest suffix of t that can be matched using s[i..n-1]. We'll compute from right. Initialize ptr = m-1 (index of last character) or better, we can compute the number of matched suffix characters. Standard: let ptr = m-1 (0-indexed), for i = n-1 downto 0: if ptr >= 0 and s[i] == t[ptr], then ptr--. Then after processing i, the number of characters matched from the end is m - (ptr+1) = m - ptr - 1? Actually if we start with ptr = m-1, we want to match t from end. When we successfully match, we decrease ptr. After processing i, ptr points to the next unmatched character from the end (the smallest index that is not yet matched). So the number of suffix characters matched = m - (ptr+1) = m - ptr - 1. But we want suf[i] = number of suffix characters that can be matched using s[i..n-1]. As we move leftwards, after processing i, we have considered s[i..n-1]. The matched suffix length is m - (ptr+1). So we can set suf[i] = m - (ptr+1) = m - ptr - 1. Example: m=3, t="abc". Start ptr=2 (points to 'c'). If we match 'c' at some position, ptr becomes 1. Then matched suffix length = 3 - (1+1) = 1? Actually after matching 'c', we have matched the last character only, so suffix length=1. That is correct: m - (ptr+1) = 3 - 2 = 1. If later match 'b', ptr becomes 0, matched suffix length = 3 - (0+1) = 2. If later match 'a', ptr becomes -1, matched suffix length = 3 - ( -1 +1) = 3. So formula works.
But careful: we need suf[i] for each i, where i is the start index. As we process from right, after processing position i, we set suf[i] = m - (ptr+1). This yields the maximum suffix length achievable using s[i..n-1] (since we greedily match from the right, which gives the maximum suffix length). Yes.
Now need to confirm correctness thoroughly.
But let's think about potential pitfalls:
- The greedy computation of pre[i] yields the maximum prefix length. But is it always the maximum? Yes, because to maximize the prefix matched from t within s[0..i], the optimal strategy is to match the earliest possible occurrences. However, is it possible that for the purpose of using position i as part of a full subsequence, we might need a specific matching for the prefix that doesn't use the greedy choice? For instance, suppose we have s="aab", t="ab". At i=1 (second 'a'), we want to see if it can be used to match t[0]? Actually t[0]='a', t[1]='b'. For i=1 (second 'a'), if we want to use it for t[0], we need to match t[1] after i. After i we have 'b', so that's possible. But greedy prefix up to i-1 (i=1, i-1=0) would match pre[0]? For s[0]='a', greedy matches t[0] so pre[0]=1. That suggests we have already matched the first character. But if we use that, then we cannot use i for t[0] because we already matched t[0]. However, we could also choose not to match t[0] with the first 'a', and instead match it with the second 'a'. But our condition L = pre[i-1] = 1 means the maximum possible prefix length before i is 1. But we need a prefix length exactly 0 (k=0) if we want to use i for t[0]. However, the condition L >= k is required. Actually if L=1, that means we can match the first character, but we don't have to use that match; we could choose to skip the first 'a' and match later. But does L=1 guarantee that there exists a subsequence of s[0..i-1] that matches exactly k characters (for k less than L)? For k = 0, can we match 0 characters? Yes, trivially. The condition L >= k is necessary? Let's re-evaluate: The requirement is that there exists a subsequence of s[0..i-1] that matches exactly the first k characters (t[0..k-1]). The existence of such a subsequence is not automatically guaranteed just because we can match up to L characters. For k < L, it might be that any subsequence that matches the first L characters necessarily uses some positions that conflict? But we only need to match exactly k characters; we can always take a prefix of the matched L-length subsequence? Not exactly: If we have a subsequence that matches the first L characters, that subsequence uses L positions. If we want to match only first k characters, we can simply take the first k of that matching (the same indices for the first k characters). Since the subsequence that matches the whole L characters includes a subsequence that matches the first k characters (just the first k matched characters). So yes, if we can match the first L characters, then for any k <= L, we can match the first k characters by taking the same indices for the first k matches. But careful: The subsequence for L characters may have some property that the k-th match occurs at some index that is after i? Actually if we take the first k matches from that subsequence, they are a subsequence of s[0..i-1]? Since the original subsequence uses indices that are all <= some index <= the last index used which is ≤ i-1 (because it's within first i-1 positions). Actually the greedy pre[i-1] is the maximum prefix length achievable using s[0..i-1]. That means there exists a subsequence (not necessarily the greedy one) that matches exactly pre[i-1] characters, all within indices ≤ i-1. For any k <= pre[i-1], there exists a subsequence matching exactly k characters (just take the first k matches of that subsequence). However, those matches might not be the earliest possible? But they are still within s[0..i-1]. So existence is assured. So condition L >= k is sufficient for existence of a subsequence matching the first k characters within s[0..i-1].
But there is a nuance: If we have L >= k, does it guarantee that we can match the first k characters without using some specific positions that might interfere with the ability to use i for the next character? But we are only concerned with existence of some subsequence that uses i for t[k]. The prefix match and the suffix match are independent; we can choose different subsequences for prefix and suffix as long as they don't use the same positions. Since prefix uses indices < i and suffix uses indices > i, they are disjoint. So we can choose any prefix subsequence that matches first k characters from s[0..i-1] and any suffix subsequence that matches last m-k-1 characters from s[i+1..n-1]. They won't conflict. So if there exists a prefix subsequence matching first k characters (which is guaranteed if L >= k) and a suffix subsequence matching last m-k-1 characters (if R >= m-k-1), and s[i] == t[k], then we can combine them with i to get a full subsequence.
Thus the condition we derived is correct.
-
The greedy computation for pre and suff gives the maximum possible lengths. But we need to know whether for a given k, the maximum prefix length L is at least k. That's exactly the condition: if L >= k, then there exists a prefix subsequence of length k. So it's fine.
-
But there is a subtlety: Could there be a scenario where the greedy pre[i-1] is maximal, but the prefix subsequence achieving that length uses the last possible indices before i in such a way that it uses the character that we might want to use for suffix? That doesn't matter because we can choose a different prefix subsequence for exactly k characters, not necessarily the greedy one. Since L >= k, there is some subsequence achieving length k. That subsequence may not be the greedy one. It exists. So fine.
-
What about the suffix side? Similarly, we rely on R = suff[i+1] being the maximum suffix length achievable using positions after i. If R >= m-k-1, then there exists a subsequence of length m-k-1 from the suffix.
Thus the condition is sufficient and necessary.
Now we must also ensure that we are not double counting the same position for both prefix and suffix. Since prefix uses indices < i, suffix uses > i, there is no overlap.
Thus the algorithm is correct.
Time complexity: O(n + m + n log m) but with binary search on lists per character. That is fine for 2e5.
But we can also do O(n + m) by precomputing for each character in t, the minimum index required? Another approach: For each position i, we can compute the "earliest" possible index in t that can be matched as the character at i based on left and right constraints. Actually we could compute for each i, the range of possible k values directly from pre[i-1] and suff[i+1], and then check if t[k] equals s[i] for some k in that range. But we can compute the nearest occurrences using arrays next and prev? Since t length is up to 2e5, we could also precompute for each i, the set of possible k quickly using the fact that intervals are contiguous. So we just need to know if there exists k with t[k]==s[i] in that interval. That's a classic query: given a character c and interval [L,R], does t contain c at any position in [L,R]? Since we have multiple queries (n up to 2e5) and alphabet size 26, we can precompute for each character an array of positions, and then for each query do binary search. That's efficient.
We could also precompute next occurrence for each character from each index, but binary search is simpler.
Implementation details:
-
Input strings: use sys.stdin.read() or input().strip().
-
Build vector positions[26] = list of indices (0-indexed) where that character appears in t.
-
Compute pre array: pre = [0]*n; ptr = 0; for i in range(n): if ptr < m and s[i] == t[ptr]: ptr += 1; pre[i] = ptr; else: pre[i] = ptr (but we need to store after each step). Actually if no match, pre[i] = ptr (unchanged). So we can just set pre[i] = ptr after processing each character.
-
Compute suff array: we need an array suff of length n+1. Initialize ptr = m-1; suff[n] = 0. Then for i in range(n-1, -1, -1): if ptr >= 0 and s[i] == t[ptr]: ptr -= 1; # after decrement, compute suff[i] = m - (ptr+1). But careful: We want suff[i] to be the maximum suffix length using s[i..n-1]. After processing s[i] and all later, the number of suffix matched = m - (ptr+1). So we can set suff[i] = m - (ptr+1). For i from n-1 downto 0.
-
However, note that if we decrement ptr after matching, then the computed suff[i] includes that match. So for i where we matched, we correctly count it.
-
For i where we didn't match, ptr remains same, so suff[i] = m - (ptr+1) is the same as suff[i+1]? Actually if no match, then the set of characters available is s[i..n-1] = s[i] plus s[i+1..n-1]. The maximum suffix length might be the same as for i+1? But if s[i] doesn't match the needed character, we cannot increase the matched suffix length. So suff[i] = suff[i+1] (since no new match). That matches our computation: if no match, ptr unchanged, suff[i] = m - (ptr+1) = suff[i+1] because suff[i+1] = m - (prev_ptr+1) and ptr same as prev_ptr. Actually careful: When iterating from right, we process i then set suff[i]. At start of loop, ptr is the value after processing i+1..n-1. So after processing i+1, ptr is at some value. Then we consider i. If no match, ptr stays same, so suff[i] = m - (ptr+1). While suff[i+1] computed earlier as m - (old_ptr+1) where old_ptr was the ptr before processing i+1? Actually we need to ensure consistency. Let's simulate: start with ptr = m-1. Then we go i=n-1 down to 0. At each step, after potential decrement, we compute suff[i] = m - (ptr+1). For i=n-1: before any match, ptr=m-1. If match, ptr becomes m-2, then suff[n-1]=m-((m-2)+1)=1. If no match, ptr stays m-1, suff[n-1]=m-((m-1)+1)=0. That is correct. Then next i=n-2: we have ptr as after processing i=n-1. So it's correct. So suff[i] correctly represents the max suffix length using s[i..n-1].
-
But note: For i where we matched, the computed suff[i] includes that match and possibly more matches from right. That is correct.
-
However, one nuance: The greedy from right gives the maximum suffix length, but does it always guarantee that we can match any suffix length up to that maximum? Yes, similar reasoning: if we can match a suffix of length p, then we can match any smaller suffix by taking the appropriate subset? Actually we need to be able to match exactly a certain number of suffix characters (like m-k-1). If we have a subsequence that matches the full suffix of length R (i.e., the last R characters of t), then we can take the last (m-k-1) characters from that subsequence? But careful: The suffix subsequence that matches the last R characters matches t[m-R .. m-1]. If we want to match the last (m-k-1) characters, which are t[k+1..m-1], we need that the suffix subsequence matches exactly that suffix. If we have a subsequence that matches the entire last R characters, it might not include the exact suffix t[k+1..m-1] if k+1 > m-R? Let's think: R is the maximum number of suffix characters we can match. That means there exists a subsequence that matches t[m-R .. m-1] (the last R characters). For a given k, we need to match t[k+1..m-1]. If k+1 is within the range [m-R, m-1], i.e., if m - (k+1) < R? Actually we need to match the last (m-k-1) characters, which is a suffix of length (m-k-1). For that to be possible using the suffix region, we need that we can match at least that many characters from the end. The existence of a subsequence matching the last R characters implies that we can also match any suffix that is a suffix of that matched suffix? Not exactly. For example, t = "ab", s = "a b". Actually simpler: Suppose t = "abca", and we consider suffix matching. Suppose we can match the full suffix of length 3: "bca". That means we can match t[1..3] = "bca". But we might need to match only "ca" (last 2). Can we match "ca" using the same region? Since we have matched "bca", we can just drop the 'b' and use the later matches for 'c' and 'a', but careful: The indices must be increasing. If we have a subsequence that matches "bca", that means we have positions i1 < i2 < i3 with s[i1]=b, s[i2]=c, s[i3]=a. To match "ca", we can take the subsequence (i2,i3) which gives "ca". That works as long as we are allowed to skip the earlier match. So yes, if we can match the suffix of length R, then for any p <= R, we can match the suffix of length p by taking the last p matches of that subsequence. However, we must ensure that those last p matches are all within the suffix region (i.e., positions > i). Since the entire subsequence uses positions > i (because it's from s[i+1..n-1]), the last p matches are also > i. So that's fine. So condition R >= m-k-1 is sufficient.
Thus everything is consistent.
Now we just need to implement.
Potential edge case: m > n? Then obviously there is no full subsequence, answer No. But our per-character check should yield all fails? Possibly some positions might satisfy conditions? Let's test: s="a", t="aa". n=1,m=2. pre: i0: ptr=0, s[0] matches t[0]? 'a' matches 'a', ptr=1, pre[0]=1. L for i0: 0? Actually i0: L=0, R = suff[1] = 0. low = max(0, 1-0) =1, high = min(1,0)=0 => fails. So no passes. So answer No. That is correct.
If m <= n, but still no subsequence, similar.
Implementation steps:
-
Read s and t.
-
n = len(s), m = len(t).
-
positions = [[] for _ in range(26)]
for idx, ch in enumerate(t): positions[ord(ch)-97].append(idx)
-
Compute pre array of length n:
pre = [0]*n
p = 0
for i,ch in enumerate(s):
if p < m and ch == t[p]:
p += 1
pre[i] = p
-
Compute suff array of length n+1:
suff = [0]*(n+1)
p = m-1
for i in range(n-1, -1, -1):
if p >= 0 and s[i] == t[p]:
p -= 1
suff[i] = m - (p+1) # p is current after processing this character
suff[n] = 0
Actually after loop, p might be something, but suff[0] already computed. And we set suff[n]=0 explicitly.
Note: If we compute inside loop, after processing i, we set suff[i]. For i=n-1 down to 0. Then after loop we can set suff[n]=0. That's fine.
-
For each i in range(n):
L = pre[i-1] if i > 0 else 0
R = suff[i+1] # since suff is length n+1, i+1 <= n, works
low = max(0, m - 1 - R)
high = min(m-1, L)
if low > high: continue to next? Actually then character not covered => answer No.
else:
c = s[i]
lst = positions[ord(c)-97]
# binary search for any element in [low, high]
# Use bisect module: idx = bisect_left(lst, low); if idx < len(lst) and lst[idx] <= high: then ok.
import bisect
pos = bisect.bisect_left(lst, low)
if pos == len(lst) or lst[pos] > high:
# not found
print("No"); return
After loop, print("Yes").
Edge Cases:
-
If m == 0? Problem says non-empty strings, so m>=1.
-
If t length is 1, then intervals become: low = max(0, 0 - R) = 0? Actually m-1=0, so low = max(0, 0 - R) = 0 (since 0-R <=0). high = min(0, L). So interval is [0,0] if L>=0 which always true. So we need t[0] == s[i] to be covered. So effectively each character must equal t[0] to be covered. But is that correct? If t is a single character, say t="a". Then subsequences of s that equal "a" are just each occurrence of 'a' in s as a subsequence of length 1. So a character is covered if it is 'a' (since it can be the subsequence itself) or if it is not 'a', can it be covered? No, because the only subsequences are those consisting of a single 'a'. So non-'a' characters cannot be part of any such subsequence. So answer Yes only if all characters of s are 'a'. That matches our condition: for each i, need s[i]==t[0] and interval [0,0] exists (which it does), and t[0] must equal s[i] else fails. So correct.
-
Another nuance: What about characters that appear in t but the interval constraints cannot be satisfied? That's fine.
Now need to test on some additional cases.
Case: s="aaa", t="aa". n=3,m=2.
pre: i0: p=0 -> match t[0] -> p=1, pre0=1
i1: p=1<2, s1='a' matches t[1] -> p=2, pre1=2
i2: p=2, pre2=2
pre=[1,2,2]
suff: compute from right. ptr=1 (t[1]='a')
i2: matches t[1], ptr=0, suff2 = 2 - (0+1)=1
i1: ptr=0, s1='a' matches t[0], ptr=-1, suff1 = 2 - (-1+1)=2
i0: ptr=-1, no match, suff0 = 2 - (-1+1)=2
suff[3]=0
Check each i:
i0: s0='a', L=0, R=suff[1]=2 => low=max(0,1-2)=0, high=min(1,0)=0 -> [0,0] t[0]='a' so ok.
i1: s1='a', L=pre0=1, R=suff[2]=1 => low=max(0,1-1)=0, high=min(1,1)=1 -> [0,1] t indices for 'a' are 0 and 1; both 'a'. So ok.
i2: s2='a', L=pre1=2, R=suff[3]=0 => low=max(0,1-0)=1, high=min(1,2)=1 -> [1,1] t[1]='a' ok.
All pass => Yes. Indeed, each 'a' can be part of some "aa" subsequence? Let's verify: s="aaa". Subsequences equal "aa": we can pick positions (1,2), (1,3), (2,3). So first character can be part of (1,2) or (1,3)? Actually first character is index1? Usually 0-index. The first 'a' can be used in subsequence picking (1,2) i.e., indices 0 and 1? Wait subsequence: positions 1 and 2 (if 1-indexed) correspond to indices 0 and 1; that uses first character. Also (1,3) uses first and third. So first character is used. Second character is used in (1,2) and (2,3). Third character is used in (1,3) and (2,3). So all covered. So Yes.
Case: s="ab", t="a". Should be Yes? Actually t="a". Only subsequence is the first character if it's 'a'. Second character 'b' cannot be part of any subsequence equal to "a". So answer should be No. Let's test: n=2,m=1.
pre: i0: p=0 matches? s0='a' matches t[0] -> p=1, pre0=1. i1: p=1 (==m) no match, pre1=1.
pre=[1,1]
suff: ptr=0 (t[0]='a')
i1: s1='b' no match, suff1 = 1 - (0+1)=0? Actually m=1, after i1: ptr=0, suff1 = 1 - (0+1)=0.
i0: s0='a' matches t[0], ptr=-1, suff0 = 1 - (-1+1)=1.
suff[2]=0
Check i0: L=0,R=suff[1]=0 => low = max(0, 0-0)=0, high = min(0,0)=0 -> [0,0], t[0]='a' matches s0='a' -> ok.
i1: s1='b', L=pre0=1,R=suff[2]=0 => low = max(0,0-0)=0, high = min(0,1)=0 -> [0,0]. t[0]='a' not 'b' -> fail. So No. Good.
Case: s="aba", t="ab". Should be Yes? Let's test: s = a b a, t= a b. Subseq: positions (1,2) uses a and b, covers first two; (1,3) uses a and a? That's "aa" not "ab". (2,3) uses b and a = "ba". So only (1,2) works. That subsequence uses only first two characters; third character 'a' is not used in any subsequence equal to "ab"? Could we use third character as the 'a'? If we try to use third as 'a', we need an 'b' after it? No after it none. Before it we need an 'a'? Actually to form "ab", we need an 'a' then 'b'. If we use third as 'a', we would need a 'b' after it, none. If we use third as 'b', it's 'a'? Actually third is 'a', so cannot be 'b'. So third character not covered. So answer should be No. Let's test algorithm:
s="aba", t="ab", n=3,m=2.
pre: i0 a: ptr=0->1, pre0=1; i1 b: ptr=1->2, pre1=2; i2 a: ptr=2 (full), pre2=2.
pre=[1,2,2]
suff: ptr=1 (t[1]='b')
i2 a: no match, ptr=1, suff2 = 2 - (1+1)=0
i1 b: matches, ptr=0, suff1 = 2 - (0+1)=1
i0 a: matches? ptr=0, t[0]='a', s0='a' matches, ptr=-1, suff0 = 2 - (-1+1)=2
suff[3]=0
Check:
i0: L=0,R=suff[1]=1 => low=max(0,1-1)=0, high=min(1,0)=0 -> [0,0], t[0]='a' matches -> ok.
i1: L=pre0=1,R=suff[2]=0 => low=max(0,1-0)=1, high=min(1,1)=1 -> [1,1], t[1]='b' matches -> ok.
i2: L=pre1=2,R=suff[3]=0 => low=max(0,1-0)=1, high=min(1,2)=1 -> [1,1], t[1]='b' but s2='a' not match -> fail. So No. Good.
Case: s="aab", t="ab". Already considered? Let's test: s = a a b, t = a b. Subseq: (1,3) uses first a and b; (2,3) uses second a and b. So first character can be used? Yes (1,3). Second character can be used (2,3). Third character (b) is used in both. So all covered => Yes.
Algorithm: n=3,m=2.
pre: i0 a: ptr0->1, pre0=1; i1 a: ptr=1 (t[1]='b')? s1='a' no match, ptr stays 1, pre1=1; i2 b: matches t[1], ptr=2, pre2=2. pre=[1,1,2]
suff: ptr=1 (t[1]='b')
i2 b: matches, ptr=0, suff2 = 2 - (0+1)=1
i1 a: ptr=0, t[0]='a', s1='a' matches, ptr=-1, suff1 = 2 - (-1+1)=2
i0 a: ptr=-1, no match, suff0 = 2 - (-1+1)=2
suff[3]=0
Check:
i0: L=0,R=suff[1]=2 => low=max(0,1-2)=0, high=min(1,0)=0 -> [0,0], t[0]='a' matches -> ok.
i1: L=pre0=1,R=suff[2]=1 => low=max(0,1-1)=0, high=min(1,1)=1 -> [0,1], t indices with 'a' are 0, so index 0 in [0,1] -> ok (s1='a' matches t[0]).
i2: L=pre1=1,R=suff[3]=0 => low=max(0,1-0)=1, high=min(1,1)=1 -> [1,1], t[1]='b' matches -> ok. All pass => Yes. Good.
Now test scenario where prefix or suffix constraints are not monotonic due to greedy? But our reasoning stands.
Let's test random small cases via brute force to confirm. We could but given the logic, I trust.
Now complexity: O(n + m + n log m) up to 2e5 * log 2e5 ~ 2e5 * 18 = 3.6M, fine.
But careful: n and m each up to 2e5, but we need to store positions for 26 letters; binary search each i. That's fine.
We must also ensure that pre and suff arrays are correct when m > n? Already handled.
Implementation pitfalls:
-
For suff array computation, we need to ensure that we correctly compute the length using m - (p+1). But after we decrement p, the new p is the index of the next character to match from the right. For example, m=3, after matching the last character, p becomes 1; then matched suffix length = 3 - (1+1) = 1. That's correct. After matching also the second last, p becomes 0; matched suffix length = 3 - (0+1) = 2. After matching first, p becomes -1; length = 3 - (-1+1)=3. So formula works.
-
However, note: In the loop from right, we must compute suff[i] after potentially matching s[i]. That means after the if condition we set suff[i] = m - (p+1). But what if p becomes -1 after matching? Then suff[i] = m - (0) = m, correct.
-
We also need to consider that p can become less than -1? No, only decrement from m-1 downwards to -1 at most.
-
For i where p already -1, we still compute suff[i] = m - ( -1+1)=m - 0 = m, which means we can match the whole t as suffix from that point onward. That is correct because we have already matched all characters to the right, so from i leftwards, we can also match the whole t if we include previous characters? But careful: suff[i] is defined as the maximum suffix length possible using s[i..n-1]. If we have already matched the entire t from the right side (i.e., p became -1), then indeed we can match the entire t from i..n-1 (since we have matched all characters using positions > i, but we might also include i? Actually after matching all, p=-1 indicates we have matched all characters from the end using positions to the right of current i? Actually the process: starting from rightmost, we match characters from the end. When p becomes -1, it means we have matched all m characters using positions we have processed so far (which are all to the right of current i). So for the current i, the suffix s[i..n-1] includes i and all to the right. Since we have already matched all m characters using positions to the right (i+1..n-1) only, the suffix from i can also match all m characters (just the same indices). So suff[i] = m is correct.
-
So for i where p = -1, suff[i] = m. That's fine.
Now check with earlier example: suff[0]=3 in "abacaba" example, consistent.
Now one more nuance: The binary search on positions list: For a given character c, if its list is empty, obviously no index.
Now about the interval: low and high are inclusive indices in t (0-indexed). We must ensure they are within [0, m-1]. Our computation already ensures that: low = max(0, m-1-R) and high = min(m-1, L). So they are within bounds.
Now let's test a more complex case where there are multiple occurrences of a character in t, and the interval may include some but not all. For example, s = "b a a b", t = "aba"? Let's try: s="baab", t="aba". n=4,m=3.
We can manually check coverage? Let's run algorithm to be safe. But more importantly, we need to ensure that our condition does not falsely accept a character when there is no actual subsequence due to ordering constraints across the whole string? But we argued that as long as we can match prefix and suffix independently, we can combine. However, we must also ensure that the prefix subsequence and the suffix subsequence do not interfere with each other regarding the relative order to i. They are on opposite sides of i, so order is fine. There is also the possibility that the prefix subsequence uses some positions that are after some positions that the suffix subsequence uses? Since prefix uses only < i and suffix uses > i, any prefix index is less than any suffix index. So the combined sequence is increasing. So good.
But what about the case where the prefix subsequence uses a position that is after some suffix position? That can't happen because prefix indices are < i and suffix indices are > i, so prefix < i < suffix, so all good.
So the combined subsequence is valid.
Now we must also ensure that the prefix subsequence we choose for length k does not use the same character that is needed for the suffix? No, they are disjoint.
Thus algorithm is correct.
Now let's consider if there is any scenario where the maximum prefix length L is >= k but any subsequence achieving that length k necessarily uses a position that is later used for the suffix? Since they are disjoint sets (prefix uses indices < i, suffix uses indices > i) there is no overlap. However, could it be that in order to match exactly k characters, we are forced to use an index that is > i? No, because we restrict to indices < i. If we can match k characters from t[0..k-1] using indices < i, that's what we need. But does L >= k guarantee that there exists a subsequence of s[0..i-1] matching exactly t[0..k-1]? We argued yes by taking the first k matches from a subsequence that matches L characters. However, that subsequence that matches L characters may use indices that are not all < i? Wait L is defined as the maximum prefix length achievable using s[0..i-1] (since pre[i-1] is based on s[0..i-1]). So any subsequence achieving L length uses only indices from 0..i-1. So its first k matches also use indices from 0..i-1. So yes.
Similarly for suffix.
Thus all good.
Now about time: O(n log m) is fine. Could also be done O(n) by precomputing for each possible k the minimum index needed? But not needed.
Now we also need to think about memory: storing pre (n ints) and suff (n+1 ints) is fine.
Now we should also check the case when m = 0 (non-empty says no). So ignore.
Now double-check with some random small brute force to be extra sure. We can produce a small brute force in mind, but we trust.
Now we should consider one more nuance: In the suffix computation, we used greedy from right to compute maximum suffix length. Is that always correct for determining maximum number of suffix characters we can match? Yes, because to match as many suffix characters as possible, we want to match them as far right as possible to leave room for earlier characters? Actually when matching suffix from the right, we want to match the last character as far right as possible? That's the standard technique: scanning from right, whenever we see the needed character from the end, we take it. This yields the maximal suffix length because we are matching characters in reverse order and always taking the rightmost possible match ensures we leave as much room as possible to match earlier characters? Actually for maximizing the number of characters matched from the end, we want to match the last character as late as possible, then the second last as late as possible but before that last match, etc. That's exactly the greedy from right. It gives the longest possible suffix that can be matched. So suff[i] is correct.
Now an edge: what if there are multiple ways to match suffix, some of which might not allow to match a smaller suffix? But as argued, if we can match a suffix of length R, we can always match any smaller suffix by dropping matches. However, dropping matches might require that the remaining matches are still in order. Since we are just taking a subsequence of the original matching indices, it's valid. So condition R >= needed is sufficient.
Now we should also check that the suffix computed as maximum length might require using positions that are very early, but for a specific needed suffix length, we might need to match characters that are not necessarily the very last ones? Actually if we need to match the last (m-k-1) characters, that is a suffix of t. If we have a way to match the last R characters (which includes those), we can use the matches corresponding to the last (m-k-1) characters from that matching, which will be a subsequence matching exactly that suffix. So fine.
Thus algorithm stands.
Now we need to implement in Python.
Potential improvements: we can combine pre and suff computations with loops.
Also note: m-1-R might be negative; we use max(0, ...).
Let's code.
Pseudo-code:
import sys, bisect
def main():
s = sys.stdin.readline().strip()
t = sys.stdin.readline().strip()
n = len(s); m = len(t)
# positions of characters in t
pos = [[] for _ in range(26)]
for i,ch in enumerate(t):
pos[ord(ch)-97].append(i)
# pre
pre = [0]*n
p = 0
for i in range(n):
if p < m and s[i] == t[p]:
p += 1
pre[i] = p
# suff
suff = [0]*(n+1) # suff[n] = 0
p = m-1
for i in range(n-1, -1, -1):
if p >= 0 and s[i] == t[p]:
p -= 1
suff[i] = m - (p+1)
# suff[n] already 0
for i in range(n):
L = pre[i-1] if i>0 else 0
R = suff[i+1] # i+1 is in range because we have n+1 size
low = max(0, m - 1 - R)
high = min(m-1, L)
if low > high:
print("No")
return
lst = pos[ord(s[i])-97]
# binary search
idx = bisect.bisect_left(lst, low)
if idx == len(lst) or lst[idx] > high:
print("No")
return
print("Yes")
if name == "main":
main()
Now test with provided examples.
Example1: abab, ab -> Yes.
Example2: abacaba, aba -> No.
Example3: abc, ba -> No.
Matches.
Now additional tests:
Case: s="a", t="a" -> n=1,m=1.
pos: [0] for 'a'.
pre: [1] (p becomes 1)
suff: p=0; i=0: s[0]=='a' matches t[0], p=-1, suff[0]=1 - (-1+1)=1? Actually m=1, after matching p=-1, suff[0] = 1 - ( -1+1) = 1 - 0 =1. suff[1]=0.
Check i=0: L=0, R=suff[1]=0, low = max(0, 0-0)=0, high = min(0,0)=0. lst for 'a' has 0, idx=0, <= high -> Yes. So output Yes, which is correct because the only character is used.
Case: s="a", t="b" -> No, as earlier.
Case: s="ab", t="ab" -> Yes? Check: i0: L=0,R=suff[1] (suff[1]? compute pre: pre[0]=1,pre[1]=2; suff: from right: p=1, i1 'b' matches => p=0, suff[1]=1 - (0+1)=0? Actually m=2, suff[1]=2-(0+1)=1? Let's compute: p starts 1. i=1: s[1]='b' matches t[1]? t[1]='b', yes, p=0; suff[1]=2 - (0+1)=1. i=0: s[0]='a' matches t[0]? p=0 and t[0]='a' yes, p=-1; suff[0]=2 - (-1+1)=2. suff[2]=0. Then i0: L=0,R=suff[1]=1 -> low=max(0,1-1)=0, high=min(1,0)=0 -> interval [0,0], t[0]='a' matches. i1: L=pre0=1,R=suff[2]=0 -> low=max(0,1-0)=1, high=min(1,1)=1 -> interval [1,1], t[1]='b' matches. So Yes. Indeed both characters are used in the only subsequence.
Case: s="aa", t="a" -> we already determined should be No because second 'a' is not used? Actually t="a". Subseq: each 'a' individually. So both characters are 'a', so each can be a subsequence itself. So each character occurs in at least one subsequence: first 'a' can be the subsequence consisting of itself; second 'a' also can be a subsequence. So answer should be Yes. Let's test: n=2,m=1.
pre: i0: p=0 matches? 'a' matches t[0] => p=1, pre0=1; i1: p=1 (full) no match, pre1=1.
suff: p=0 (t[0]='a')
i1: 'a' matches t[0], p=-1, suff[1] = 1 - (-1+1)=1? Actually m=1: after i1, p=-1, suff[1] = 1 - ( -1+1) = 1 - 0 = 1.
i0: p=-1, s0='a'? but p=-1, condition false, p stays -1, suff[0] = 1 - (-1+1)=1.
suff[2]=0.
Now i0: L=0, R=suff[1]=1 => low = max(0, 0-1) = 0? m-1 = 0, so low = max(0,0-1)=0; high = min(0,0)=0 -> interval [0,0], t[0]='a' matches -> ok.
i1: L=pre0=1, R=suff[2]=0 => low = max(0, 0-0)=0, high = min(0,1)=0 -> interval [0,0], t[0]='a' matches -> ok. So Yes. Good.
Case: s="ab", t="a" -> we already did, second character not 'a', so No. Good.
Now test with s length 200000, but fine.
Now we must consider that the pre and suff arrays can be computed using ints.
Potential problem: The suff array computation as described sets suff[i] = m - (p+1) after processing s[i]. But at i = n-1, we set suff[n-1]; then for i = n-2, etc. But we also need suff[n] = 0. That's fine. However, note that the value of suff[i+1] used for position i is from the computed suff array; we must ensure that suff[i+1] reflects the maximum suffix length using s[i+1..n-1]. Our loop sets suff for all indices from n-1 down to 0, so suff[i+1] is already computed when i < n-1? Actually when i = n-1, we need suff[n] which is 0. For i < n-1, i+1 is > i, and since we computed from right, we have already computed suff[i+1] before? We compute from n-1 down to 0, so when we are at i, we have already processed i+1? Actually if we loop for i in range(n-1, -1, -1), then we process indices in decreasing order: first i=n-1, then i=n-2, ..., i=0. So when we are at i, we have already computed suff[i+1]? Not yet, because i+1 > i, and we are going downwards: i+1 is larger, we would have processed i+1 earlier because the loop starts at n-1 and goes down, so i+1 (which is greater than i) would be processed earlier. Example: n=5, loop order: i=4,3,2,1,0. When i=3, we have already processed i=4, so suff[4] is known. suff[4] corresponds to index 4. suff[i+1] for i=3 is suff[4] known. Good. So after the loop, we have suff[0..n-1] computed. For i = n-1, we need suff[n] which we set separately to 0. So that's fine.
Thus algorithm correct.
Now consider if there is a case where the character s[i] appears in t but the interval [low, high] may contain indices where t[k] = s[i] but those indices might not be usable because of ordering constraints with the chosen prefix and suffix? For example, suppose we match prefix of length k using some indices before i, but that prefix subsequence may end with an index that is after some index that would be needed for the suffix? Not an issue because prefix indices < i < suffix indices.
But there could be a more subtle issue: The prefix subsequence that matches exactly k characters might use some indices that are after the suffix subsequence's indices? Not possible because suffix indices are > i. So no conflict.
Thus fine.
Now we should consider memory and speed: using Python, with n up to 200k, loops and binary searches are okay.
One more test: s = "ab", t = "ab". Already done.
Another: s = "abcde", t = "ace". Is every character covered? Let's think: t = "a c e". Subseq of s that equals "ace": (1,3,5) = a,c,e. So characters at positions 1,3,5 are covered. Characters at positions 2 (b) and 4 (d) are not covered. So answer should be No. Let's test algorithm.
n=5,m=3.
pre: p=0
i0 a: match a -> p=1, pre0=1
i1 b: no (t[1]='c')? b!=c, so p stays 1, pre1=1
i2 c: matches t[1]='c' -> p=2, pre2=2
i3 d: p=2 (t[2]='e') d!=e, pre3=2
i4 e: matches -> p=3, pre4=3
pre=[1,1,2,2,3]
suff: from right, ptr=2 (t[2]='e')
i4 e: matches, ptr=1, suff4 = 3 - (1+1)=1
i3 d: ptr=1, t[1]='c'? d!=c, ptr=1, suff3 = 3 - (1+1)=1
i2 c: matches t[1]='c', ptr=0, suff2 = 3 - (0+1)=2
i1 b: ptr=0, t[0]='a'? b!=a, ptr=0, suff1 = 3 - (0+1)=2
i0 a: matches t[0], ptr=-1, suff0 = 3 - (-1+1)=3
suff[5]=0
Now check each i:
i0: L=0,R=suff[1]=2 => low=max(0,2-2)=0, high=min(2,0)=0 -> [0,0], t[0]='a' matches -> ok.
i1: L=pre0=1,R=suff[2]=2 => low=max(0,2-2)=0, high=min(2,1)=1 -> [0,1]. s1='b'. t[0]='a', t[1]='c'. No 'b' -> fail. So No. i1 fails. So answer No.
i2: L=pre1=1,R=suff[3]=1 => low=max(0,2-1)=1, high=min(2,1)=1 -> [1,1], t[1]='c' matches -> ok.
i3: L=pre2=2,R=suff[4]=1 => low=max(0,2-1)=1, high=min(2,2)=2 -> [1,2], s3='d', t[1]='c', t[2]='e' -> no d -> fail.
i4: L=pre3=2,R=suff[5]=0 => low=max(0,2-0)=2, high=min(2,2)=2 -> [2,2], t[2]='e' matches -> ok.
Thus fails as expected.
Now test: s="ababa", t="aba". We suspect all characters might be covered? Let's think: s: a b a b a. t= a b a. Subseq occurrences: positions (1,2,3) -> a b a covers 1,2,3; (1,2,5) -> a b a covers 1,2,5; (1,4,5) -> a b a? Actually (1,4,5) -> a a a? No, (1,4,5) = a b a? s1=a, s4=b, s5=a => a b a yes covers 1,4,5; (3,4,5) -> a b a covers 3,4,5; (1,2,3) and (3,4,5) etc. Also (1,2,5), (1,4,5), (3,4,5) maybe also (1,2,3). So positions: 1 used in many, 2 used? In (1,2,3) and (1,2,5), yes. 3 used in (1,2,3) and (3,4,5). 4 used in (1,4,5) and (3,4,5). 5 used in (1,2,5), (1,4,5), (3,4,5). So all covered => Yes.
Check algorithm:
n=5,m=3.
pre: p=0
i0 a: match -> p=1, pre0=1
i1 b: match t[1]? t[1]='b' -> p=2, pre1=2
i2 a: match t[2]? t[2]='a' -> p=3, pre2=3
i3 b: p=3 -> no match, pre3=3
i4 a: pre4=3
pre=[1,2,3,3,3]
suff: from right ptr=2 (t[2]='a')
i4 a: matches, ptr=1, suff4 = 3-(1+1)=1
i3 b: matches t[1]='b', ptr=0, suff3 = 3-(0+1)=2
i2 a: matches t[0]='a', ptr=-1, suff2 = 3-(-1+1)=3
i1 b: ptr=-1, no match, suff1 = 3-(-1+1)=3
i0 a: suff0 = 3
suff[5]=0
Now per i:
i0: L=0,R=suff[1]=3 => low=max(0,2-3)=0, high=min(2,0)=0 -> [0,0], t[0]='a' matches -> ok.
i1: L=pre0=1,R=suff[2]=3 => low=max(0,2-3)=0, high=min(2,1)=1 -> [0,1], s1='b', t[1]='b' in interval -> ok.
i2: L=pre1=2,R=suff[3]=2 => low=max(0,2-2)=0, high=min(2,2)=2 -> [0,2], s2='a', t has 'a' at 0 and 2, both in interval -> ok.
i3: L=pre2=3,R=suff[4]=1 => low=max(0,2-1)=1, high=min(2,3)=2 -> [1,2], s3='b', t[1]='b' in [1,2] -> ok.
i4: L=pre3=3,R=suff[5]=0 => low=max(0,2-0)=2, high=min(2,3)=2 -> [2,2], s4='a', t[2]='a' in interval -> ok.
All pass => Yes.
Now test where t has repeated characters: s="aaaa", t="aa". Already did: Yes.
Now test where t length > s length: s="abc", t="abcd". m=4,n=3. pre: maximum pre[2] will be 3? Actually t="abcd". pre: i0 a matches -> p=1, i1 b matches -> p=2, i2 c matches -> p=3, pre last=3 < 4. suff: from right ptr=3, i2 c != d, so ptr=3, suff2=4-(3+1)=0, i1 b: ptr=3, no match, suff1=0, i0 a: suff0=0? Actually after processing, ptr still 3, suff[0]=4-(3+1)=0. So all L,R small. For each i: For i0: L=0,R=suff[1]=0, low=max(0,3-0)=3, high=min(3,0)=0 -> low>high -> fail. So all fail => No. Good.
Now test scenario where the character appears in t but interval excludes all its occurrences. Example: s="bac", t="abc". n=3,m=3. s: b a c. t: a b c? Actually "abc" order a,b,c. Subseq of s equal "abc"? b a c -> order b,a,c not a,b,c; a,b,c? s has a at index1, b at index0, c at index2. To get "abc", we need a before b before c. Not possible. So answer No. Check algorithm: Compute pre: i0 b matches? t[0]='a'? No -> pre0=0; i1 a matches t[0]? yes -> p=1, pre1=1; i2 c matches t[1]? t[1]='b'? no; matches t[1]? no; p stays 1, pre2=1. pre=[0,1,1]
suff: from right ptr=2 (t[2]='c')
i2 c matches? yes, ptr=1, suff2=3-(1+1)=1
i1 a: ptr=1, t[1]='b'? a!=b, ptr=1, suff1=3-(1+1)=1
i0 b: ptr=1, t[1]='b'? b matches, ptr=0, suff0=3-(0+1)=2
suff[3]=0
Now i0: s0='b', L=0,R=suff[1]=1 -> low=max(0,2-1)=1, high=min(2,0)=0 -> low>high -> fail.
i1: s1='a', L=pre0=0,R=suff[2]=1 -> low=max(0,2-1)=1, high=min(2,0)=0 -> fail.
i2: s2='c', L=pre1=1,R=suff[3]=0 -> low=max(0,2-0)=2, high=min(2,1)=1 -> low>high -> fail. All fail => No.
Now consider a case where a character appears in t and interval includes an occurrence, but that occurrence might be the only one and maybe it's used for prefix or suffix in a conflicting way? But as argued, we can choose independent subsequences. So fine.
Now consider potential off-by-one errors in the calculation of low and high with 0-index. Let's re-derive carefully:
We have t[0..m-1]. We want to use i to match t[k] (0 <= k <= m-1). Then need:
-
a subsequence from s[0..i-1] that matches t[0..k-1] (prefix of length k). So need existence of such subsequence. This is possible iff the maximum prefix length achievable with s[0..i-1] is at least k. Let L = max prefix length using s[0..i-1]. So condition: L >= k.
-
a subsequence from s[i+1..n-1] that matches t[k+1..m-1] (suffix of length m-k-1). This is possible iff the maximum suffix length achievable with s[i+1..n-1] is at least m-k-1. Let R = max suffix length using s[i+1..n-1] (i.e., we can match the last R characters of t from that suffix). But careful: The suffix length we can match is the number of characters from the end of t. However, we need to match specifically t[k+1..m-1]. That's the last m-k-1 characters. So we need to be able to match a suffix of length m-k-1. So condition: R >= m-k-1.
Now, we used R as the maximum suffix length using s[i+1..n-1]. This is the number of trailing characters of t that can be matched. So indeed, we need R >= m-k-1 => k >= m - 1 - R.
Thus low = m - 1 - R, high = L.
But also k must be between 0 and m-1 inclusive. So low_clamped = max(0, m-1-R), high_clamped = min(m-1, L).
Also need s[i] = t[k].
So correct.
Now check when i=0: L = 0. So high = min(m-1, 0) = 0. low = max(0, m-1 - R). So interval is [low, 0] if low <=0, else empty. So i=0 can only be used to match t[0] if low <=0, i.e., m-1 - R <= 0 => R >= m-1. So for the first character to be usable, we need that the suffix after it can match at least m-1 characters, i.e., almost whole t except possibly the first character. That makes sense: if we use position 0 to match t[0], we need to match the rest (t[1..]) using later positions. So R must be >= m-1. Indeed. So algorithm matches intuition.
Similarly for i = n-1: R = 0, so low = max(0, m-1 - 0) = m-1, high = min(m-1, L). So interval is [m-1, L] if L >= m-1, else empty. So we need L >= m-1 and t[k] = s[i] with k=m-1, i.e., last character. So last character must match t[m-1] and we must be able to match the prefix before it of length m-1. That makes sense.
Now, we should also note that pre[i-1] might be less than L? Actually we defined L = pre[i-1] which is maximum prefix length using s[0..i-1]. That is correct.
But one nuance: In the computation of pre, we used greedy left-to-right which gives the maximum prefix length. However, what if there exists a subsequence matching a prefix of length exactly k, but the greedy algorithm matched more than k using indices before i, but the matches for the first k characters might be such that the k-th match is at an index that is greater than or equal to i? Actually if greedy matched L >= k, the first k matches are within the first i-1 characters? Not necessarily! The greedy algorithm builds a subsequence by scanning s and matching t sequentially. The matches occur at indices where characters match. The greedy subsequence's indices are increasing, and when we stop at i-1, the last matched index among the first L matches might be ≤ i-1? Actually the greedy algorithm processes s[0..i-1] and produces matches at some indices all ≤ i-1. So the k-th match (for k ≤ L) is definitely at some index ≤ i-1 because it occurs when processing the segment up to i-1. So it's fine. So we can use the greedy subsequence's first k matches as a valid prefix subsequence.
Thus fine.
Now we might think about an alternative: compute for each i the earliest index in t that can be matched as the character at i and the latest index, but binary search is simpler.
Now about performance: Since we call bisect for each i, it might be slower but acceptable.
But we could also compute for each i directly the set of possible k values by using the pre and suff values to determine the range and then checking if t[L] or something. However, because t may have multiple occurrences of a character, we need to check if any occurrence lies in the interval. So binary search is necessary.
Now implement.
Potential Pitfall: The suff array calculation uses m - (p+1). But what about when p is -1? Then p+1 = 0, so suff[i] = m. That's correct.
But we also need to handle the case where p becomes less than -1? No.
Now we can code.
After writing code, test with the examples and a few additional.
Now consider if there is a scenario where the character s[i] matches t[k] for some k in interval, but the prefix subsequence we choose (first k matches) and the suffix subsequence we choose (last m-k-1 matches) might conflict with each other because the prefix subsequence might use some positions that are after some positions used by suffix subsequence? No, because prefix positions < i < suffix positions. So no conflict.
But one more subtlety: The suffix subsequence we choose from s[i+1..n-1] that matches t[k+1..m-1] might require that the first character of that suffix (t[k+1]) is matched at some position j > i. The prefix subsequence ends at some position p < i. Since p < i < j, order is fine. So combined subsequence indices are increasing.
Thus fine.
Now about the condition for existence of a subsequence matching the suffix t[k+1..m-1] from s[i+1..n-1]. We used R = maximum suffix length from that region. But we need to match exactly the suffix of length m-k-1, which is a suffix of t. However, the maximum suffix length might be, say, R = m-k-1 + something. That's fine. But we also need to ensure that the matched suffix is exactly the suffix starting at index k+1, not just any suffix of length m-k-1. But note: The suffix of t of length m-k-1 is uniquely t[k+1..m-1]. When we say we can match a suffix of length p from the right, that means we can match t[m-p..m-1]. To match t[k+1..m-1], we need p = m-k-1 and we need to match exactly the suffix starting at k+1, which is the same as matching the last m-k-1 characters. So yes, if we can match the last m-k-1 characters, that is exactly t[k+1..m-1]. So it's consistent.
Thus algorithm is correct.
Now we need to ensure we don't run into time limit: O(n log m) is fine.
But we can also do O(n) by precomputing for each i the range of possible k values and then using prefix arrays to check if for character c there is any occurrence of that character in t within that range. Since there are only 26 characters, we can precompute for each character a boolean prefix sum over t indices: for each character, we could have an array of size m+1 where cum[c][i] = number of occurrences of character c in t[0..i-1]. Then for a given interval [low,high], we can check if cum[c][high+1] - cum[c][low] > 0 in O(1). This would be O(n * 26? Actually we need only the character s[i], so O(1) per query. That would be more efficient and avoid binary search overhead. However, building cumulative arrays for 26 characters of length m each would be O(26m) = 5.2e6, which is fine. And then each query is O(1). That could be even simpler and possibly faster in Python? Might be a bit more memory (26 * (m+1) ints) ~ 5.2 million ints, each 28 bytes? That could be ~150 MB, which is too high. Actually in Python, storing as list of ints for each character would be memory heavy. A 2D list of 26 lists each of length m+1 with integers: each integer is an object (28 bytes). 5.2e6 * 28 = ~145 MB, plus overhead, might exceed memory limit (typical 256 MB maybe okay? But risky). Also Python list overhead per inner list. Could be near limit. Better to use array('I') or use list of bytearray? But we need counts up to m (2e5), so need at least 4 bytes per count if using array. Could use list of array('I') from module array, which stores C integers. That would be 4 bytes per entry, 5.2e64 = 20.8 MB, plus overhead, acceptable. But using built-in list of ints may be too memory intensive. However, we could also use a 2D list where each inner list is a list of ints; each int is a Python object, memory heavy. So better to stick with binary search (log factor) which uses only positions lists for each character, which store only the indices where character appears. That memory is at most m ints (2e5) which is small. Binary search per i is log(freq) and overall fast enough.
Given constraints, binary search is simpler and memory-friendly. Python's bisect on list of ints is fast; 200k * log2(200k) ~ 200k * 18 = 3.6 million operations, each binary search does some comparisons and list accesses. That should be well within time (2 seconds maybe fine). We'll use that.
But we need to be careful about recursion? Not needed.
Now think about one more nuance: The intervals low and high are computed using L and R. But L and R are based on greedy maximum lengths. However, it's possible that L >= k but the specific subsequence achieving prefix length k might use the last index before i, and that might affect something? Not relevant.
But what about the case where the greedy suffix from the right might not allow to match exactly the required suffix if we choose a different prefix? We already argued that independence holds.
Alright.
Now we can finalize the solution.
But before finalizing, let's consider a case where the character s[i] appears in t multiple times, and the interval includes some of those indices, but maybe using that index k forces the prefix to be of length k and suffix of length m-k-1. However, it's possible that even though L >= k and R >= m-k-1, there is no way to simultaneously pick prefix and suffix that are compatible with each other? For them to be compatible, they must not use the same indices (they are on opposite sides of i). But could it be that the chosen prefix subsequence that matches t[0..k-1] uses some indices that are also needed for the suffix subsequence? That's impossible because prefix indices are < i and suffix indices are > i. So no overlap.
Thus fine.
Now we can proceed to implement.
But let's double-check the suff calculation: In the loop, after we decrement p if match, we compute suff[i] = m - (p+1). However, what if we matched and then p becomes -1? Then p+1=0, suff[i]=m. That's correct. But what about the case where we didn't match: p remains same, suff[i] = m - (p+1). That gives the same value as suff[i+1]? Because for i+1 we had p' (the value before processing i+1) maybe? Let's verify: At iteration for i, p is the value after processing i+1..n-1. That is exactly the pointer state before considering s[i]. After possibly matching s[i], p may be decremented. Then suff[i] = m - (p+1) reflects the number of suffix characters matched using s[i..n-1]. Meanwhile, suff[i+1] (which was computed earlier) would be m - (p_before_i+1) where p_before_i is the pointer before processing i+1? Actually careful: The loop goes: for i from n-1 downto 0: at entry, p corresponds to the state after processing indices > i (i.e., after i+1..n-1). That means p = pointer value after matching from the right up to i+1. Then if we match at i, we decrement p. So after processing i, the pointer is p' (maybe decremented). So the number of matched suffix characters after including i is m - (p' + 1). That's suff[i]. For i+1, which we processed in a previous iteration, when we processed i+1, we had a different p state at entry (call it p_i+1_entry) and after processing i+1 we set suff[i+1] = m - (p_i+1_exit+1). Now p_i+1_exit is the pointer after processing i+1, which is exactly the p we have at entry for i (since we've processed all indices > i). So p_entry_i = p_i+1_exit. Therefore suff[i+1] = m - (p_entry_i + 1). And suff[i] = m - (p_exit_i + 1). So they are consistent: suff[i] >= suff[i+1] (since p_exit_i <= p_entry_i). Good.
Thus our R = suff[i+1] = m - (p_entry_i + 1). That is the number of suffix characters matched using s[i+1..n-1] (since entry state p_entry_i corresponds to after processing i+1..n-1, i.e., matching from that region). So that's exactly the maximum suffix length using positions > i. So correct.
Now we should also think about the case where we have multiple possible subsequences and we need to ensure that for a given i, there exists at least one full subsequence using i. Our condition is sufficient. Is it necessary? Suppose there exists a full subsequence using i. Then let k be the index of t matched by i. Then obviously s[i] = t[k]. Also, the part before i forms a subsequence matching t[0..k-1] using indices < i, so L (max prefix length using indices < i) must be at least k. Similarly, after i matches t[k+1..], so R must be at least m-k-1. So the condition holds. So it's necessary and sufficient. Therefore algorithm exactly determines coverage.
Thus final answer.
We'll implement with sys.stdin.read().splitlines() for speed.
Potential pitfalls: We must ensure that we convert characters to indices quickly. Using ord(c)-97 is fine.
Now we'll write final code.
Edge Cases: If t length is 0? Not given.
Now we can also early exit if we find a failing character.
Now double-check the computation of low/high with 0-index: low = max(0, m-1 - R) where R = suff[i+1] (integer). high = min(m-1, L). Good.
Now confirm with some random tests via mental simulation.
Test: s="cabab", t="ab". Should be? Let's see: s: c a b a b. t="ab". Subseq occurrences: after c, we can pick a and b at positions (2,3) or (2,5) etc. Which characters are covered? c: can it be part of an "ab" subsequence? Only if it matches a or b, but it's c, so no. So answer No. Let's test algorithm:
n=5,m=2.
pre: i0 c: p=0 (t[0]='a') no match, pre0=0
i1 a: match t[0] -> p=1, pre1=1
i2 b: match t[1] -> p=2, pre2=2
i3 a: p=2 (full), pre3=2
i4 b: pre4=2
pre=[0,1,2,2,2]
suff: from right ptr=1 (t[1]='b')
i4 b: matches, ptr=0, suff4 = 2 - (0+1)=1
i3 a: ptr=0, t[0]='a' matches, ptr=-1, suff3 = 2 - (-1+1)=2
i2 b: ptr=-1, no match, suff2 = 2 - (-1+1)=2
i1 a: ptr=-1, no match, suff1 = 2
i0 c: ptr=-1, no match, suff0 = 2
suff[5]=0
Now check each i:
i0: s0='c', L=0,R=suff[1]=2 -> low=max(0,1-2)=0, high=min(1,0)=0 -> interval [0,0], t[0]='a' not 'c' -> fail. So answer No. Good.
Now test with s where every character appears but one cannot be used because it would require a prefix/suffix that doesn't exist: Example2 already.
Now consider scenario where t contains only one character and s has many of that character, then all characters that are that character are covered; characters not that character are not covered. So if s has any other character, answer No.
Alright.
Now implement.
One more thing: We used pre[i-1] for L. But if i=0, we set L=0. That's fine.
Now we must ensure that we compute pre array correctly when m=0? Not needed.
Now, I'll write final answer.
But before finalize, check for potential integer overflow? Not in Python.
Now, there is a nuance: The problem statement uses 1-indexing, but we use 0-index.
Now produce final solution explanation and code.
Given the analysis, the answer is to implement as above.
Now I'll produce final response. ```python
import sys
import bisect
def solve() -> None:
data = sys.stdin.read().splitlines()
if not data:
return
s = data[0].strip()
t = data[1].strip()
n = len(s)
m = len(t)
# positions of each character in t
pos = [[] for _ in range(26)]
for idx, ch in enumerate(t):
pos[ord(ch) - 97].append(idx)
# pre[i] = longest prefix of t that is a subsequence of s[0..i]
pre = [0] * n
p = 0
for i, ch in enumerate(s):
if p < m and ch == t[p]:
p += 1
pre[i] = p
# suff[i] = longest suffix of t that is a subsequence of s[i..n-1]
# we also need suff[n] = 0
suff = [0] * (n + 1)
p = m - 1
for i in range(n - 1, -1, -1):
if p >= 0 and s[i] == t[p]:
p -= 1
suff[i] = m - (p + 1) # number of matched suffix characters
# suff[n] is already 0
for i in range(n):
L = pre[i - 1] if i > 0 else 0
R = suff[i + 1] # characters strictly after i
low = max(0, m - 1 - R)
high = min(m - 1, L)
if low > high:
print("No")
return
lst = pos[ord(s[i]) - 97]
# does lst contain any index in [low, high]?
j = bisect.bisect_left(lst, low)
if j == len(lst) or lst[j] > high:
print("No")
return
print("Yes")
if name == "main":
solve()