qyliang/CodeNanoFix huggingface dataset:
Buggy code:
S = input()
T = input()

dic = {chr(s): [] for s in range(ord("a"), ord("z") + 1)}
for i, s in enumerate(S):
    dic[s].append(i)

out = 0
cnt = 0
V = [0] * len(T)
for i, t in enumerate(T):
    if dic[t]:
        for l in dic[t]:
            if cnt < l:
                V[i] = l + 1
                cnt = l
                break
        else:
            cnt = dic[t][0]
            V[i] = cnt + 1
    else:
        out = 1
        break
if out == 1:
    print(-1)
else:
    cnt = 0
    for i in range(len(V)-1):
        if V[i+1] < V[i]:
            cnt += 1
    if cnt == 0:
        print(V[-1])
    elif V[-1] == len(S):
        print(len(S) * (cnt + 1))
    else:
        print

Fixed code:
import bisect
S = input()
T = input()

dic = {chr(s): [] for s in range(ord("a"), ord("z") + 1)}
for i, s in enumerate(S):
    dic[s].append(i + 1)

out = 0
cnt = -1
V = [0] * len(T)
for i, t in enumerate(T):
    if dic[t]:
        """
        for l in dic[t]:
            if cnt < l:
                V[i] = l
                cnt = l
                break
        else:
            cnt = dic[t][0]
        """

        idx = bisect.bisect(dic[t], cnt)
        if idx == len(dic[t]):
            V[i] = dic[t][0]
            cnt = dic[t][0]
        else:
            V[i] = dic[t][idx]
            cnt = dic[t][idx]

    else:
        out = 1
        break
if out == 1:
    print(-1)
else:
    cnt = 0
    for i in range(len(V)-1):
        if V[i+1] <= V[i]:
            cnt += 1
    if cnt == 0:
        print(V[-1])
    elif V[-1] == len(S):
        print(len(S) * (cnt + 1))
    else:
        print(len(S) * cnt + V[-1])
a = 0
Buggy code:
from bisect import bisect_right, bisect_left
s = [ord(i) - ord("a") for i in input()]
t = [ord(i) - ord("a") for i in input()]
index = [[] for _ in range(26)]

for i, x in enumerate(s):
    index[x].append(i)


l_s = len(s)

for x in index:
    if x:
        x.append(x[0] + l_s)

print(index)

cnt = 0
for r in t:
    seq = index[r]
    if not seq:
        cnt = -2
        break
    i = cnt % l_s
    j = bisect_right(seq, i)
    cnt += seq[j] - i

print(cnt+1)




Fixed code:
from bisect import bisect_right, bisect_left
s = [ord(i) - ord("a") for i in input()]
t = [ord(i) - ord("a") for i in input()]
index = [[] for _ in range(26)]

for i, x in enumerate(s):
    index[x].append(i)


l_s = len(s)

for x in index:
    if x:
        x.append(x[0] + l_s)

cnt = -1
for r in t:
    seq = index[r]
    if not seq:
        cnt = -2
        break
    i = cnt % l_s
    j = bisect_right(seq, i)
    cnt += seq[j] - i

print(cnt+1)
Buggy code:
from itertools import *
from collections import *
from bisect import *
s=list(input())
t=input()
sets=set(s)

a=len(s)*len(t)
maxcount=len(s)
for i in range(100):
    if maxcount<a
        maxcount**=10
    else:
        break


for T in t:
    if not T in sets:
        print(-1)
        exit()
N=len(s)
dic={}
for i in range(N):
    if not s[i] in dic:
        dic.update({s[i]:[i]})
    else:
        dic[s[i]].append(i)

count=1
prei=0
for i in range(len(t)):
    idx=bisect_left(dic[t[i]],prei)
    if idx<len(dic[t[i]]):
        prei=dic[t[i]][idx]
    else:
        prei=dic[t[i]][0]
        count+=N
    if i==len(t)-1:
        count+=prei



if count<=maxcount:
    print(count)
else:
    print(-1)


Fixed code:
from itertools import *
from collections import *
from bisect import *
s=list(input())
t=input()
sets=set(s)
"""
a=len(s)*len(t)
maxcount=len(s)
for i in range(100):
    if maxcount<a:
        maxcount**=10
    else:
        break
"""

for T in t:
    if not T in sets:
        print(-1)
        exit()
N=len(s)
dic={}
for i in range(N):
    if not s[i] in dic:
        dic.update({s[i]:[i]})
    else:
        dic[s[i]].append(i)

count=0
prei=0
for i in range(len(t)):
    idx=bisect_left(dic[t[i]],prei)
    if idx<len(dic[t[i]]):
        prei=dic[t[i]][idx]+1
    else:
        prei=dic[t[i]][0]+1
        count+=N
    if i==len(t)-1:
        count+=prei



if count<=len(s)*(10**100):
    print(count)
else:
    print(-1)
Buggy code:
s = input()
t = input()
d = {i:[] for i in "abcdefghijklmnopqrstuvwxyz"}
for i in range(len(s)):
    d[s[i]].append(i)
for i in t:
    if len(d[i]) == 0:
        print(-1)

def binary_search(arr, k):
    l, r = 0, len(arr)-1
    while l <= r:
        m = l+r >> 1
        if arr[m] < k:
            l = m+1
        else:
            r = m-1
    return l

a, b = 0, 0
for i in t:
    j = binary_search(d[i], a)
    if len(d[i]) <= j:
        a = 0
        b += 1
        j = binary_search(d[i], 0)
    a = d[i][j] + 1
print(b * len(s) + a)


Fixed code:
s = input()
t = input()
d = {i:[] for i in "abcdefghijklmnopqrstuvwxyz"}
for i in range(len(s)):
    d[s[i]].append(i)
for i in t:
    if len(d[i]) == 0:
        print(-1)
        exit()

def binary_search(arr, k):
    l, r = 0, len(arr)-1
    while l <= r:
        m = l+r >> 1
        if arr[m] < k:
            l = m+1
        else:
            r = m-1
    return l

a, b = 0, 0
for i in t:
    j = binary_search(d[i], a)
    if len(d[i]) <= j:
        a = 0
        b += 1
        j = binary_search(d[i], 0)
    a = d[i][j] + 1
print(b * len(s) + a)
Buggy code:
s = input()
t = input()
lent = len(t)
tcount = 0
snow = 0
count = 0
flag = True
while True:
    if tcount == lent:
        break
    if t[tcount] not in s:
        flag = False
        break
    snow = s.index(t[tcount])
    tcount += 1
    if t[tcount] in s[snow+1:]:
        snow = s[snow+1:].index(t[tcount])
    else:
        snow = s.index(t[tcount])
        count += 1
if flag:
    print(count*len(s) + s.index(t[-1])+1)
else:
    print(-1)

Fixed code:
s = input()
t = input()
lent = len(t)
tcount = 0
snow = 0
count = 0
flag = True
if t[0] not in s:
    print(-1)
else:
    snow = s.index(t[0])
    while True:
        tcount += 1
        if tcount == lent:
            break
        if t[tcount] in s[snow+1:]:
            snow = s[snow+1:].index(t[tcount])+snow+1
        elif t[tcount] in s:
            snow = s.index(t[tcount])
            count += 1
        else:
            flag = False
            break
    if flag:
        print(count*len(s) + snow+1)
    else:
        print(-1)
Buggy code:
from string import ascii_lowercase
from bisect import bisect_right
s=input()
t=input()
p={c: [] for c in s}
for x,c in enumerate(s):
    p[c].append(x)
z=0
l=-1
for c in t:
    if c not in p:
        print(-1)
        break
    x=bisect_right(p[c],1)
    if x==len(p[c]):
        x=0
        z+=1
    l=p[c][x]

print(z*len(s)+l+1)


Fixed code:
from string import ascii_lowercase
from bisect import bisect_right
s = input()
t = input()
p = {c: [] for c in s}
for x, c in enumerate(s):
    p[c].append(x)
z = 0
l = -1
for c in t:
    if c not in p:
        print(-1)
        break
    x = bisect_right(p[c], l)
    if x == len(p[c]):
        x = 0
        z += 1
    l = p[c][x]
else:
    print(z * len(s) + l + 1)
Buggy code:
from bisect import bisect_left
s, t = [ord(c)-ord('a') for c in input()], [ord(c)-ord('a') for c in input()]
cnt = [[] for i in range(26)]
for i in range(len(s)):
    cnt[s[i]].append(i)
    cnt[s[i]].append(i+len(s))
ans = 0
p = 0
for i in range(len(t)):
    c = t[i]
    if (len(cnt[c]) == 0):
        # print('-1')
        exit()
    next = bisect_left(cnt[c], p)
    p = cnt[c][next] + 1
    if (p >= len(s)):
        p -= len(s)
        ans += len(s)
ans += p
print(ans) 


Fixed code:
from bisect import bisect_left
s, t = [ord(c)-ord('a') for c in input()], [ord(c)-ord('a') for c in input()]
cnt = [[] for i in range(26)]
for i in range(len(s)):
    cnt[s[i]].append(i)
    cnt[s[i]].append(i+len(s))
for i in range(26):
    cnt[i] = sorted(cnt[i])
ans = 0
p = 0
for i in range(len(t)):
    c = t[i]
    if (len(cnt[c]) == 0):
        print('-1')
        exit()
    next = bisect_left(cnt[c], p)
    p = cnt[c][next] + 1
    if (p >= len(s)):
        p -= len(s)
        ans += len(s)
ans += p
print(ans)
Buggy code:
from bisect import*
s=input()
t=input()
if set(t)<=set(s):
    d={}
    for k,j in enumerate(s*2):
        if j in d:
            d[j].append(k)
        else:
            d[j]=[k]
    print(d)
    now=-1
    ans=0
    for _,i in enumerate(t):
        nex=d[i][bisect_right(d[i],now)]
        ans+=nex-now

        #print(i,_,now,nex,ans)
        
        if nex>=len(s):
            now=nex-len(s)
        else:
            now=nex
    print(ans)

else:
    print(-1)



Fixed code:
from bisect import*
s=input()
t=input()
if set(t)<=set(s):
    d={}
    for k,j in enumerate(s*2):
        if j in d:
            d[j].append(k)
        else:
            d[j]=[k]
    #print(d)
    now=-1
    ans=0
    for _,i in enumerate(t):
        nex=d[i][bisect_right(d[i],now)]
        ans+=nex-now

        #print(i,_,now,nex,ans)
        
        if nex>=len(s):
            now=nex-len(s)
        else:
            now=nex
    print(ans)

else:
    print(-1)
Buggy code:
from collections import defaultdict
from bisect import bisect_left

s = input()
n = len(s)
s = s + s
t = input()
if set(t) - set(s):
	print(-1)
	exit()
d = defaultdict(list)
for i in range(2 * n):
	d[s[i]] += [i]
cur = tot = 0
for c in t:
	x = d[c][bisect_left(d[c], cur + 1)]
	if x < n:
		cur = x
	else:
		cur = x - n
		tot += n
if cur == n - 1:
	cur = 0
tot += cur
print(tot)

Fixed code:
from collections import defaultdict
from bisect import bisect_left

s = input()
n = len(s)
s = s + s
t = input()
if set(t) - set(s):
	print(-1)
	exit()
d = defaultdict(list)
for i in range(2 * n):
	d[s[i]] += [i]
cur = tot = 0
for c in t:
	x = d[c][bisect_left(d[c], cur)]
	if x < n:
		cur = x + 1
	else:
		cur = x - n + 1
		tot += n
tot += cur
print(tot)
Buggy code:
import bisect
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

S = str(input())
T = str(input())
N = len(S)
cou = int(0)
cur = int(0)
move = int(0)
S_map = [[]for _ in range(26)]

for i in range(N):
    S_map[ord(S[i])-97].append(i+1)
for i in range(N):
    S_map[ord(S[i])-97].append(N+i+1)

for c in T:
    print(cur)
    if S_map[ord(c)-97]==[]:
        ans = -1
        break
    move = bisect.bisect_left(S_map[ord(c)-97],cur+1)
    cur = S_map[ord(c)-97][move]
    if cur>=N:
        cou += 1
        cur -= N

if ans==0:
    ans = cou*N + cur

print(ans)

Fixed code:
import bisect
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

S = str(input())
T = str(input())
N = len(S)
cou = int(0)
cur = int(0)
move = int(0)
S_map = [[]for _ in range(26)]

for i in range(N):
    S_map[ord(S[i])-97].append(i+1)
for i in range(N):
    S_map[ord(S[i])-97].append(N+i+1)

for c in T:
#    print(cur)
    if S_map[ord(c)-97]==[]:
        ans = -1
        break
    move = bisect.bisect_left(S_map[ord(c)-97],cur+1)
    cur = S_map[ord(c)-97][move]
    if cur>=N:
        cou += 1
        cur -= N

if ans==0:
    ans = cou*N + cur

print(ans)
Buggy code:
s = input()
t = input()
s_map = [[] for _ in range(26)]
import bisect
for i in range(len(s)):
    s_map[ord(s[i])-97].append(i)
for i in range(len(s)):
    s_map[ord(s[i])-97].append(len(s)+i)
cur = 0
count = 0
f = True
for c in t:
    if s_map[ord(c)-97] == []:
        f = False
        break
    cur = s_map[ord(c)-97][bisect.bisect_left(s_map[ord(c)-97],cur)]
    print(cur)
    if cur >= len(s):
        count += 1
        cur -= len(s)
print(count * len(s) + cur+1 if f else -1)

Fixed code:
s = input()
t = input()
s_map = [[] for _ in range(26)]
import bisect
for i in range(len(s)):
    s_map[ord(s[i])-97].append(i)
for i in range(len(s)):
    s_map[ord(s[i])-97].append(len(s)+i)
cur = 0
count = 0
f = True
for c in t:
    if s_map[ord(c)-97] == []:
        f = False
        break
    np = bisect.bisect_left(s_map[ord(c)-97],cur)
    cur = s_map[ord(c)-97][np]+1
    
    if cur >= len(s):
        count += 1
        cur -= len(s)
print(count * len(s) + cur if f else -1)
Buggy code:
import sys
input = sys.stdin.readline
import bisect
S = input()
E = {S[i]:[] for i in range(len(S)-1)}
for i in range(len(S)-1):
  E[S[i]].append(i)
T = input()
#print(E)
a = len(S)-1
ans=0
now = 0
for i in range(len(T)):
  if not T[i] in E.keys():
    print(-1)
    exit()
  s=E[T[i]]
  b= bisect.bisect_right(s,now)
  #print(s,b,now)
  if b==len(s):
    ans+=s[0]+1+a-1-now
    now=s[0]
  else:
    ans+=s[b]-now
    now=s[b]
print(ans+1)
      
    
    
  

Fixed code:
import sys
input = sys.stdin.readline
import bisect
S = input()
E = {S[i]:[] for i in range(len(S)-1)}
for i in range(len(S)-1):
  E[S[i]].append(i)
T = input()
a = len(S)-1
ans=0
now = -1
for i in range(len(T)-1):
  if not T[i] in E.keys():
    print(-1)
    exit()
  s=E[T[i]]
  b= bisect.bisect_right(s,now)
  #print(s,b,now)
  if b==len(s):
    ans+=s[0]+1+a-1-now
    now=s[0]
  else:
    ans+=s[b]-now
    now=s[b]
print(ans)
Buggy code:
import bisect

S = input()
T = input()

Sset = set(list(S))
Tset = set(list(T))
if len(Tset-Sset) > 0:
  print(-1)
  exit()

SS = S * 2
SSpos = {}
for i in range(len(SS)):
  if SS[i] in SSpos:
    SSpos[SS[i]].append(i)
  else:
    SSpos[SS[i]] = [i]
print(SSpos)

ans = 1
i = 0
for t in T:
  j = bisect.bisect_left(SSpos[t],i)
  ans += SSpos[t][j] - i
  #print(t,SSpos[t],ans,i,j)
  i = SSpos[t][j]
  if i >= len(S):
    i -= len(S)
print(ans)


Fixed code:
import bisect

S = input()
T = input()

Sset = set(list(S))
Tset = set(list(T))
if len(Tset-Sset) > 0:
  print(-1)
  exit()

SS = S * 2
SSpos = {}
for i in range(len(SS)):
  if SS[i] in SSpos:
    SSpos[SS[i]].append(i)
  else:
    SSpos[SS[i]] = [i]
for i in SSpos:
  SSpos[i].sort()
#print(SSpos)

ans = 0
i = 0
for t in T:
  j = bisect.bisect_left(SSpos[t],i)
  ans += SSpos[t][j] - i + 1
  #print(t,SSpos[t],ans,i,j)
  i = SSpos[t][j] + 1
  if i >= len(S):
    i -= len(S)
print(ans)
Buggy code:

# import sys
# input = sys.stdin.readline
import bisect


def ch_to_int(c):
    return ord(c) - 96


def main():
    s = input()
    t = input()
    pos = 0
    count = 0
    ch = [[] for i in range(27)]

    for i in range(len(s)):
        ch[ch_to_int(s[i])].append(i)

    for i in range(len(t)):
        c = ch_to_int(t[i])
        li = ch[c]

        if len(li) == 0:
            pos = -2
            count = 0
            break

        next_index = bisect.bisect_left(li, pos)
        # print(next_index, pos, li)
        if next_index >= len(li):
            next_pos = li[0]
            count += 1
            pos = next_pos + 1
        else:
            if pos == li[next_index]:
                pos += 1
            else:
                pos = li[next_index] + 1
        # print(li, next_index, pos, count)

    print(pos + count * len(s))


if __name__ == '__main__':
    main()


Fixed code:

# import sys
# input = sys.stdin.readline
import bisect


def ch_to_int(c):
    return ord(c) - 96


def main():
    s = input()
    t = input()
    pos = 0
    count = 0
    ch = [[] for i in range(27)]

    for i in range(len(s)):
        ch[ch_to_int(s[i])].append(i)

    for i in range(len(t)):
        c = ch_to_int(t[i])
        li = ch[c]

        if len(li) == 0:
            pos = -1
            count = 0
            break

        next_index = bisect.bisect_left(li, pos)
        # print(next_index, pos, li)
        if next_index >= len(li):
            next_pos = li[0]
            count += 1
            pos = next_pos + 1
        else:
            if pos == li[next_index]:
                pos += 1
            else:
                pos = li[next_index] + 1
        # print(li, next_index, pos, count)

    print(pos + count * len(s))


if __name__ == '__main__':
    main()
Buggy code:
import bisect
s = input()
t = input()
d = { c: [] for c in t }
for i, c in enumerate(s):
  if c in d:
    d[c].append(i)
for dc in d.values():
  dc.sort()

n = -1
ans = 0


for j, c in enumerate(t):
  l = d[c]
  if l:
    i = bisect.bisect_right(l, n)
    if i == len(l):
      ans += len(s)
      n = -1
    else:
      n = l[i]
    if j == len(t) - 1:
      if i == len(l):
        ans += l[0] + 1
      else:
        ans += l[i]+1
  else:
    ans = -1
    break

print(ans)


Fixed code:
import bisect
s = input()
t = input()
d = { c: [] for c in t }
for i, c in enumerate(s):
  if c in d:
    d[c].append(i)

n = -1
ans = 0


for j, c in enumerate(t):
  l = d[c]
  if l:
    i = bisect.bisect_right(l, n)
    if i == len(l):
      ans += len(s)
      n = l[0]
    else:
      n = l[i]
    if j == len(t) - 1:
      if i == len(l):
        ans += l[0] + 1
      else:
        ans += l[i]+1
  else:
    ans = -1
    break

print(ans)
Buggy code:
import bisect
s = input()
t = input()
d = { c: [] for c in t }
for i, c in enumerate(s):
  if c in d:
    d[c].append(i)
for dc in d.values():
  dc.sort()
n = 0
ans = 0

for j, c in enumerate(t):
  l = d[c]
  if l:
    i = bisect.bisect_right(l, n)
    if i == len(l):
      ans += i
      n = l[0]
    elif j == len(t) - 1:
      ans += i
    else:
      n = l[i]
  else:
    ans = -1
    break

print(ans)

  
    

Fixed code:
import bisect
s = input()
t = input()
d = { c: [] for c in t }
for i, c in enumerate(s):
  if c in d:
    d[c].append(i)
for dc in d.values():
  dc.sort()

n = -1
ans = 0


for j, c in enumerate(t):
  l = d[c]
  if l:
    i = bisect.bisect_right(l, n)
    if i == len(l):
      ans += len(s)
      n = l[0]
    else:
      n = l[i]
    if j == len(t) - 1:
      if i == len(l):
        ans += l[0] + 1
      else:
        ans += l[i]+1
  else:
    ans = -1
    break

print(ans)
Buggy code:
def slove():
    import sys
    import collections
    import bisect
    input = sys.stdin.readline
    s = str(input().rstrip('\n'))
    t = str(input().rstrip('\n'))
    d = collections.defaultdict(list)
    for i in range(len(s)):
        d[s[i]] += [i]
    loop = 0
    cur = 2
    for i in range(len(t)):
        p = bisect.bisect_left(d[t[i]], cur)
        if p == 0:
            print(-1)
            exit()
        elif p > len(d[t[i]]):
            loop += 1
            cur = d[t[i]][0] + 1
    print(len(s) * loop + cur)


if __name__ == '__main__':
    slove()


Fixed code:
def slove():
    import sys
    import collections
    import bisect
    input = sys.stdin.readline
    s = str(input().rstrip('\n'))
    t = str(input().rstrip('\n'))
    d = collections.defaultdict(list)
    for i in range(len(s)):
        d[s[i]] += [i]
    loop = 0
    cur = 0
    for i in range(len(t)):
        p = bisect.bisect_left(d[t[i]], cur)
        if p == 0 and len(d[t[i]]) == 0:
            print(-1)
            exit()
        elif p == len(d[t[i]]):
            loop += 1
            cur = d[t[i]][0] + 1
        else:
            cur = d[t[i]][p] + 1
    print(len(s) * loop + cur)


if __name__ == '__main__':
    slove()
Buggy code:
slist = input()
tlist = input()
scounter = 0
slength=len(slist)
slist=slist+slist
preposition=-1
for tchar in tlist:
    preposition=slist.find(tchar, preposition+1)
    if preposition==-1:
        scounter=-1
        break
    elif preposition>slength:
        scounter+=slength
        preposition-=slength
    print(tchar, scounter, preposition)
if (scounter==-1):
    print(scounter)
else:
    print(scounter+preposition+1)

Fixed code:
slist = input()
tlist = input()
scounter = 0
slength=len(slist)
slist=slist+slist
preposition=-1
for tchar in tlist:
    preposition=slist.find(tchar, preposition+1)
    if preposition==-1:
        scounter=-1
        break
    elif preposition>=slength:
        scounter+=slength
        preposition-=slength
    # print(tchar, scounter, preposition)
if (scounter==-1):
    print(scounter)
else:
    print(scounter+preposition+1)
Buggy code:
import sys

s = input()
t = input()
s_len = len(s)
t_len = len(t)
index_list = [0]*t_len
rt1 = 0
rt2 = 0
s = s + s
for m in range(t_len):
	rt1 = s.find(t[m],rt1)
	if rt1 == -1:
		print(rt1)
		sys.exit()
	else:
		#index_list[m] = rt1
		rt1 += 1
		if rt1 < s_len:
			pass
		else:
			rt2 += 1

rt = rt1 + rt2 * s_len
print(rt)

Fixed code:
import sys

s = input()
t = input()
s_len = len(s)
t_len = len(t)
index_list = [0]*t_len
rt1 = 0
rt2 = 0
s = s + s
for m in range(t_len):
	rt1 = s.find(t[m],rt1)
	if rt1 == -1:
		print(rt1)
		sys.exit()
	else:
		#rt1 = rt1 % s_len + 1
		#index_list[m] = rt1
		if rt1 < s_len:
			pass
		else:
			rt1 = rt1 % s_len
			rt2 += 1
	rt1 += 1
rt = rt1 + rt2 * s_len
print(rt)
Buggy code:
from collections import defaultdict
import bisect
s = input()
t = input()
ls = len(s)
dbls = s*2
idx = 0
ans = 0
dic = defaultdict(list)

for i, c in enumerate(dbls):
    dic[c] += [i+1]

print(dbls)

for c in t:
    if not c in dic.keys():
        print(-1)
        exit()
    lst = dic[c]
    i = bisect.bisect_right(lst, idx)
    idx = lst[i]
    if idx > ls:
        ans += ls
        idx -= ls
    else:
        idx = lst[i]

if not idx == 0:
    ans += idx
print(ans)


Fixed code:
from collections import defaultdict
import bisect
s = input()
t = input()
ls = len(s)
dbls = s*2
idx = 0
ans = 0
dic = defaultdict(list)

for i, c in enumerate(dbls):
    dic[c] += [i+1]

for c in t:
    if not c in dic.keys():
        print(-1)
        exit()
    lst = dic[c]
    i = bisect.bisect_right(lst, idx)
    idx = lst[i]
    if idx > ls:
        ans += ls
        idx -= ls
    else:
        idx = lst[i]

if not idx == 0:
    ans += idx
print(ans)
Buggy code:
s = input()
t = input()

s_ = s[:]

ti = 0
ans = 0
flag = True

while ti < len(t):
    si = s_.find(t[ti])
    
    if si != -1:
        ans += si + 1
        s_ = s_[si+1:]
        ti += 1
        flag = False
    else:
        if flag:
            print(-1)
            break
        ans += len(s_)
        s_ = s[:]
        flag = True
        
print(ans)

Fixed code:
s = input()
t = input()

s_ = s[:]

ti = 0
ans = 0
flag = True

while ti < len(t):
    si = s_.find(t[ti])
    
    if si != -1:
        ans += si + 1
        s_ = s_[si+1:]
        ti += 1
        flag = False
    else:
        if flag:
            print(-1)
            exit(0)
        ans += len(s_)
        s_ = s[:]
        flag = True
        
print(ans)
Buggy code:
import bisect
S = input()
T = input()
D = [[] for _ in range(26)]
for i in range(26):
  D[i] = [0] * S.count(chr(97 + i))
P = [0] * 26
for i, s in enumerate(S):
  n = ord(s) - 97
  D[n][P[n]] = i + 1
  P[n] += 1
pos = 0
loop = 0
for t in T:
  n = ord(t) - 97
  if D[n] == []:
    print(-1)
    break
  _pos = bisect.bisect(D[n], pos)
  if _pos == len(D[n]):
    loop += 1
  pos = D[n][pos % len(D[n])]
else:
  print(len(S) * loop + pos)

Fixed code:
import bisect
S = input()
T = input()
D = [[] for _ in range(26)]
for i in range(26):
  D[i] = [0] * S.count(chr(97 + i))
P = [0] * 26
for i, s in enumerate(S):
  n = ord(s) - 97
  D[n][P[n]] = i + 1
  P[n] += 1
pos = 0
loop = 0
for t in T:
  n = ord(t) - 97
  if D[n] == []:
    print(-1)
    break
  _d = bisect.bisect(D[n], pos)
  _pos = D[n][_d % len(D[n])]
  if _pos <= pos:
    loop += 1
  pos = _pos
else:
  print(len(S) * loop + pos)