The only difference between easy and hard versions is the length of the string.

You are given a string $s$ and a string $t$, both consisting only of lowercase Latin letters. It is guaranteed that $t$ can be obtained from $s$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $s$ without changing order of remaining characters (in other words, it is guaranteed that $t$ is a subsequence of $s$).

For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".

You want to remove some substring (contiguous subsequence) from $s$ of maximum possible length such that after removing this substring $t$ will remain a subsequence of $s$.

If you want to remove the substring $s[l;r]$ then the string $s$ will be transformed to $s_1 s_2 \dots s_{l-1} s_{r+1} s_{r+2} \dots s_{|s|-1} s_{|s|}$ (where $|s|$ is the length of $s$).

Your task is to find the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$.


-----Input-----

The first line of the input contains one string $s$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters.

The second line of the input contains one string $t$ consisting of at least $1$ and at most $2 \cdot 10^5$ lowercase Latin letters.

It is guaranteed that $t$ is a subsequence of $s$.


-----Output-----

Print one integer — the maximum possible length of the substring you can remove so that $t$ is still a subsequence of $s$.


-----Examples-----
Input
bbaba
bb

Output
3

Input
baaba
ab

Output
2

Input
abcde
abcde

Output
0

Input
asdfasdf
fasd

Output
3

Difficulty: introductory

Starter code:

Solutions:
import sys
input = sys.stdin.readline
s = input()
t = input()
l, r = [0]*len(t), [0]*len(t)
li, ri = 0, len(s) - 1
for i in range(len(t)):
    while s[ li ] != t[ i ]:
        li += 1
    while s[ ri ] != t[ - i - 1 ]:
        ri -= 1
    l[ i ] = li
    r[ -i -1 ] = ri
    li += 1
    ri -= 1

print( max( [ r[ 0 ], len(s) - l[ -1 ] - 1 ] + [ max( 0, r[ i ] - l[ i - 1 ] ) - 1 for i in range( 1, len(t) ) ] ) )


s=str(input())
t=str(input())
i=0
j=0
arr1=[]
while(i<len(s)):
	if(s[i]==t[j]):
		arr1.append(i+1)
		j+=1
	i+=1
	if(j==len(t)):
		break

i=len(s)-1
j=len(t)-1
arr2=[0]*len(t)
while(i>=0):
	if(s[i]==t[j]):
		arr2[j]=i+1
		j-=1
	i-=1
	if(j==-1):
		break

ans=max(arr2[0]-1,len(s)-arr1[-1])

for i in range(len(t)-1):
	ans=max(ans,arr2[i+1]-arr1[i]-1)

print(ans)



from sys import stdin
s=stdin.readline().strip()
s1=stdin.readline().strip()
dp=[-1 for i in range(len(s1)+1)]
dp1=[-1 for i in range(len(s1)+1)]
x=0
for i in range(len(s)):
    if s[i]==s1[x]:
        dp[x+1]=i
        x+=1
        if x>=len(s1):
            break
x=len(s1)-1
y=1
dp[0]=-1
dp1[0]=len(s)
for i in range(len(s)-1,-1,-1):
    if s[i]==s1[x]:
        dp1[y]=i
        x-=1
        y+=1
        if x<0:
            break

ans=0
for i in range(len(s1)+1):
    ans=max(dp1[len(s1)-i]-dp[i]-1,ans)
    ans=max(dp[len(s1)-i]-dp1[i]-1,ans)
    
        
print(ans)


s = input()
t = input()
n = len(s)
m = len(t)
l = [-1]
r = [n]
c = 0
for i in range(n):
    if s[i] == t[c]:
        l.append(i)
        c += 1
        if c >= m:
            break
c = 0
for i in range(n):
    if s[-1-i] == t[-1-c]:
        r.append(n-1-i)
        c += 1
        if c >= m:
            break
mx = 0
for i in range(m+1):
    mx = max(mx, r[m-i]-l[i]-1)
print(mx)


s = input()
t = input()
lefts = [0 for i in range(len(t))]
rights = [0 for i in range(len(t))]

i = 0
j = 0
while i < len(s) and j < len(t):
	if (s[i] == t[j]):
		lefts[j] = i
		j += 1
	i += 1

i = len(s) - 1
j = len(t) - 1 
while i >= 0 and j >= 0:
	if (s[i] == t[j]):
		rights[j] = i
		j -= 1
	i -= 1
#print(lefts)
#print(rights)
ans = max(len(s) - lefts[-1] - 1, rights[0])
for i in range(len(t) - 1):
	ans = max(ans, rights[i + 1] - lefts[i] - 1)
print(ans)

s = input()
t = input()


def compute_prefix(s, t):
    prefix = [0 for i in range(len(t) + 1)]
    i = 0
    j = 0
    while i < len(t):
        while s[j] != t[i]:
            j += 1
        prefix[i+1] = j+1
        i += 1
        j += 1
    return prefix


prefix = compute_prefix(s, t)
suffix = compute_prefix(s[::-1], t[::-1])
res = 0
for i in range(len(t) + 1):
    res = max(res, len(s) - (prefix[i] + suffix[len(t) - i]))
print(res)


s = input()
t = input()

i = 0
l = []
for c in t:
    while s[i] != c:
        i += 1
    l.append(i)
    i += 1

i = len(s)-1
r = []
for c in t[::-1]:
    while s[i] != c:
        i -= 1
    r.append(i)
    i -= 1

r.reverse()

mx = r[0]
for i in range(len(t)-1):
    mx = max(mx, r[i+1] - l[i] - 1)
mx = max(mx, len(s)-l[-1]-1)

print(mx)

S = input()
T = input()
N, M = len(S), len(T)

def calc(s, t):
    X = [0] * len(s)
    j = 0
    for i in range(len(s)):
        if j >= len(t):
            X[i] = j
        elif s[i] == t[j]:
            X[i] = j+1
            j += 1
        else:
            X[i] = X[i-1]
    return [0] + X

        
A, B = calc(S, T), calc(S[::-1], T[::-1])[::-1]
# print(A, B)
l, r = 0, N
while r-l>1:
    m = (l+r)//2
    C = [A[i]+B[i+m] for i in range(N-m+1)]
    # print("m, C =", m, C)
    if max(C) >= M:
        l = m
    else:
        r = m
print(l)


s = input()
t = input()

pos = [[-1, -1] for i in range(len(t))]

ptr = 0
for i,c in enumerate(t):
    while s[ptr] != c:
        ptr += 1
    pos[i][0] = ptr
    ptr += 1

ptr = len(s) - 1
for i in range(len(t)-1, -1, -1):
    c = t[i]
    while s[ptr] != c:
        ptr -= 1
    pos[i][1] = ptr
    ptr -= 1

best = max(pos[0][1], len(s)-pos[-1][0]-1)
for i in range(1, len(pos)):
    best = max(best, pos[i][1] - pos[i-1][0] - 1)

print(best)


from sys import stdin, stdout

input = stdin.readline

s, t = list(map(str, stdin.read().split()))

n, m = len(s), len(t)
a = [0] * m
b = [0] * m

pos = 0
for i in range(n):
    if s[i] == t[pos]:
        a[pos] = i
        pos += 1
        if pos == m:
            break

pos = m - 1
for i in range(n - 1, -1, -1):
    if s[i] == t[pos]:
        b[pos] = i
        pos -= 1
        if pos == -1:
            break

res = max(b[0], n - (a[-1] + 1))
for i in range(m - 1):
    res = max(res, (b[i + 1] - 1) - (a[i] + 1) + 1)

print(res)


s = input()
t = input()
n = len(s)
m = len(t)
le = []
ri = []
ans = 0
p = 0
for i in range(n):
    if p < m:
        if s[i] == t[p]:
            le.append(i)
            if p == m - 1:
                ans = max(ans, n - 1 - i)
            p += 1
p = m - 1
for j in range(n - 1, -1, -1):
    if p >= 0:
        if s[j] == t[p]:
            ri.append(j)
            if p == 0:
                ans = max(ans, j - 0)
            p -= 1
ri.reverse()
for k in range(m - 1):
    ans = max(ans, ri[k + 1] - le[k] - 1)
print(ans)

s = input()
t = input()
fp = [-1]
tp = 0
for i in range(len(s)):
    if s[i] == t[tp]:
        fp.append(i)
        tp += 1
        if tp == len(t):
            break
lp = [len(s)]
tp = len(t) - 1
for i in range(len(s) -1, -1 , -1):
    if s[i] == t[tp]:
        lp.append(i)
        tp -= 1
        if tp == -1:
            break
lp.reverse()
print(max([ (l-f) for (l,f) in zip(lp, fp)]) - 1)

s=input()
t=input()
l=0
pre=[]
suf=[]
for i in range(len(s)):
    if(s[i]==t[l]):
        pre.append(i)
        l+=1
    if(l==len(t)):
        break
l=len(t)-1
for i in range(len(s)-1,-1,-1):
    if(s[i]==t[l]):
        suf.append(i)
        l-=1
    if(l==-1):
        break
pre.sort()
suf.sort()
ans=0

ans = max(suf[0],len(s)-pre[-1]-1)

for i in range(len(t)-1):
    ans = max(ans,abs(suf[i+1]-pre[i]-1))

print(ans)


s = input()
t = input()

f, b = [], []

cur = len(t) - 1
for i in range(len(s) - 1, -1, -1):
    if s[i] == t[cur]:
        b.append(i)
        cur -= 1
    if cur == -1:
        break

b.sort()

cur = 0
for i in range(len(s)):
    if s[i] == t[cur]:
        f.append(i)
        cur += 1
    if cur == len(t):
        break



ans = max(b[0], len(s) - f[-1] - 1)
bbb = 0
bbb ^= 1

bbb += 1

for i in range(len(f) - 1):
    ans = max(ans, abs(b[i +1] -1 - f[i]))

print(ans)


'''input
asdfasdf
fasd




'''
import sys
from collections import defaultdict as dd

mod=10**9+7

def ri(flag=0):
	if flag==0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())



st = input()
pat = input()
n= len(st)

forward = [0 for i in range(n)]

idx =0

for i in range(n):
	if idx < len(pat) and st[i] == pat[idx]:
		idx+=1
		forward[i] = idx

backward = [0 for x in range(n)]

idx =len(pat)-1

for i in range(n-1,-1,-1):
	if idx>=0 and st[i] == pat[idx]:
		idx-=1
		backward[i]= idx+2

# print(forward)
# print(backward)

c1 =dd(int)
c2 = dd(int)

for i in range(n):
	c1[forward[i]] = i+1
	c2[backward[i]] = i+1

ans = max(c2[1]-1 , n-c1[len(pat)])



#print(c1,c2)
for i in range(1,len(pat)):
	ans = max(ans , abs(c1[i] - c2[i+1])-1)

print(ans)

from sys import stdin
input = stdin.readline

s = input()
t = input()

n = len(t)
low = [0 for i in range(len(t))]
high = [0 for i in range(len(t))]

acc = 0

for i in range(n):
    acc_t = t[i]
    
    while acc_t != s[acc]: acc += 1
    low[i] = acc
    acc += 1

acc = len(s)-1
    
for i in range(len(t)):
    acc_t = t[n-i-1]
    
    while acc_t != s[acc]: acc -= 1
    high[n-i-1] = acc
    acc -= 1
    
res = max(high[0], len(s)-low[n-1]-1)

for i in range(n-1):
    res = max(res, high[i+1]-low[i]-1)
    
print(res)

# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
# TAIWAN NUMBER ONE!!!!!!!!!!!!!!!!!!!
from sys import stdin, stdout
import math
from math import gcd,sqrt

#T = int(input())
#N = int(input())
s1 = input()
N = len(s1)
s2 = input()
M = len(s2)
#print(N)
#N,M,Q = [int(x) for x in stdin.readline().split()]
#arr = [int(x) for x in stdin.readline().split()]

asc = []
dsc = []

k = 0
for i in range(len(s1)):
    if s1[i]==s2[k]:
        asc.append(i)
        k += 1
    if k==M:
        break

k = M-1
for i in range(len(s1)-1,-1,-1):
    if s1[i]==s2[k]:
        dsc.append(i)
        k -= 1
    if k==-1:
        break
        
dsc = dsc[::-1]
#print(N,M)
#print(asc,dsc)
res = 0
for i in range(M-1):
    d = dsc[i+1] - asc[i] - 1
    res = max(d,res)
    
res = max(res,asc[0])
res = max(res,dsc[0])
res = max(res,N-1-asc[-1])
res = max(res,N-1-dsc[-1])

        
print(res)


# s = "tessts"
# t = "tet"
s = input()
t = input()
ns = len(s)
nt = len(t)

ans = 0
found = 0

DP_left = [0]*ns

DP_left[0] = int(s[0] == t[0])
for i in range(1, ns):
	if DP_left[i-1] < nt:
		DP_left[i] = DP_left[i-1] + (s[i] == t[DP_left[i-1]])

DP_right = [0]*ns

DP_right[-1] = int(s[-1] == t[-1])

for i in reversed(list(range(ns-1))):
	if 0 <= nt - DP_right[i+1] - 1 < nt :
		DP_right[i] = DP_right[i+1] + (s[i] == t[nt - 1 - DP_right[i+1]])

from collections import defaultdict

d_r = defaultdict(int)
d_l = defaultdict(int)

old = 1

for index, i in enumerate(DP_left):
	if i == old:
		d_l[i] = index
		old += 1

for index, i in enumerate(DP_right):
	d_r[i] = max(d_r[i], index)

for i in range(nt + 1):

	if i == 0:
		ans = max(ans, d_r[nt])
	elif i == nt:
		ans = max(ans, ns - d_l[nt] - 1)
	else:
		ans = max(ans, d_r[nt-i] - d_l[i] - 1)
print(ans)


import sys
from collections import defaultdict
#input = sys.stdin.readline
import math


def main():
    s = input()
    t = input()
    longest_prefix = [0] * len(s)
    longest_suffix = [0] * len(s)

    longest_prefix[0] = 1 if s[0] == t[0] else 0
    for i in range(1, len(s)):
        x = longest_prefix[i-1]
        if x == len(t) or s[i] != t[x]:
            longest_prefix[i] = x
        else:
            longest_prefix[i] = x + 1

    longest_suffix[-1] = 1 if s[-1] == t[-1] else 0
    for i in range(len(s)-2, -1, -1):
        x = longest_suffix[i+1]
        if x == len(t) or s[i] != t[-1-x]:
            longest_suffix[i] = x
        else:
            longest_suffix[i] = x + 1

    res = 0

    pos1 = 0
    while longest_prefix[pos1] != len(t):
        pos1 += 1
    res = max(res, len(s)-1-pos1)

    pos2 = len(s)-1
    while longest_suffix[pos2] != len(t):
        pos2 -= 1
    res = max(res, pos2)


    pos1 = 0
    pos2 = 0
    while pos1 < len(s):
        while pos2+1 < len(s) and longest_prefix[pos1] + longest_suffix[pos2+1] >= len(t):
            pos2 += 1
        res = max(res, pos2-pos1-1)
        pos1 += 1

    print(res)


def __starting_point():
    main()


__starting_point()

s=input()
t=input()

"""pos=[]
for i in range(26):
    h=[]
    pos.append(h)

for i in range(0,len(t)):
    for j in range(0,len(s)):
        if(s[j]==t[i]):
            pos[i].append(j)"""

L1=[]
L2=[]

ptr=0
for i in range(0,len(s)):
    if(s[i]==t[ptr]):
        L1.append(i)
        ptr+=1
    if(ptr>=len(t)):
        break

ptr=len(t)-1
for i in range(len(s)-1,-1,-1):
    if(s[i]==t[ptr]):
        L2.append(i)
        ptr-=1
    if(ptr<0):
        break

L2=L2[::-1]

#print(L1)
#print(L2)

mx=-1
for i in range(0,len(L1)-1):
    mx=max(mx,L2[i+1]-L1[i]-1)

mx=max(mx,L2[0])
mx=max(mx,len(s)-L1[-1]-1)

print(mx)




s=input()
t=input()
indl=[-1 for i in range(len(t))]
j=0
for i in range(len(s)):
	if s[i]==t[j]:
		indl[j]=i
		j+=1
		if j==len(t):
			break
indr=[-1 for i in range(len(t))]
j=len(t)-1
for i1 in range(len(s)):
	i=len(s)-1-i1
	if s[i]==t[j]:
		indr[j]=i
		j-=1
		if j==-1:
			break
l=indr[0]
for i in range(len(t)-1):
	l=max(l,indr[i+1]-indl[i]-1)
l=max(l,len(s)-1-indl[len(t)-1])
print(l)

def int_multiple():
    return  [int(c) for c in input().split()]

def int_single():
    return int(input())

def str_multiple():
    return [c for c in input().split()]

def str_single():
    return input()

# start

s = str_single()
t = str_single()


ti = 0

start = []
for si in range(len(s)):
    if(s[si] == t[ti]):
        start.append(si)
        ti += 1
        if ti == len(t):
            break

end = []
ti = len(t) - 1
for si in reversed(list(range(len(s)))):
    if (s[si] == t[ti]):
        end.append(si)
        ti -= 1
        if ti < 0:
            break

end = list(reversed(end))

start_mx = len(s) - start[-1] - 1
end_mx = end[0]
mx_between = 0
for i in range(len(t)-1):
    diff = end[i+1] - start[i] - 1
    mx_between = max(mx_between, diff)

print(max(start_mx, end_mx, mx_between))