qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import sys
import numpy as np
import bisect
s = input()
t = input()



count = 0
s_len = len(s)
s_list = np.zeros(len(s))
t_list = []*len(t)
indexes = []

for i in range(26):
  
  indexes.append([j for j, x in enumerate(s) if x == chr(i + ord('a')) ])
  

for i,k in enumerate(t):

  index = indexes[ord(k)-97]
  
  if len(indexes) == 0:
    print(-1)
    sys.exit(0)
  else:
    t_list.insert(i,index)
    


pre = -1
for i in range(len(t)):
  
  #high = [j  for j in t_list[i] if j > pre]
  high = bisect.bisect_left(t_list[i], pre) 

  if high >= len(t_list[i]):
    
    count += s_len - pre  + t_list[i][0]
    pre = t_list[i][0] 
  else:
    
    count += t_list[i][high] - pre 
    pre = t_list[i][high]

print(count)

Fixed code:
import sys
import numpy as np
import bisect
s = input()
t = input()



count = 0
s_len = len(s)
s_list = np.zeros(len(s))
t_list = []*len(t)
indexes = []

for i in range(26):
  
  indexes.append([j for j, x in enumerate(s) if x == chr(i + ord('a')) ])
  


for i,k in enumerate(t):

  index = indexes[ord(k)-ord('a')]
  
  if len(index) == 0:
    print(-1)
    sys.exit(0)
  else:
    t_list.insert(i,index)
    


pre = -1
for i in range(len(t)):
  
  #high = [j  for j in t_list[i] if j > pre]
  high = bisect.bisect_left(t_list[i], pre+1) 

  if high >= len(t_list[i]):
    
    count += s_len - pre  + t_list[i][0]
    pre = t_list[i][0] 
  else:
    
    count += t_list[i][high] - pre 
    pre = t_list[i][high]

print(count)
Buggy code:
#E
import bisect

s = input()
t = input()
lens = len(s)

s_n = {}
for i in range(len(s)):
    if s[i] in s_n:
        s_n[s[i]].append(i+1)
    else:
        s_n[s[i]] = [i+1]
        
print(s_n)

d1=0
d2=0
for i in t:
    if not(i in s_n):
        print(-1)
        break
    else:
        if d1 >= s_n[i][-1]:
            d1 = s_n[i][0]
            d2 += lens
        else:
            index = bisect.bisect(s_n[i],d1)
            d1 = s_n[i][index]
else:
    print(d1+d2)



Fixed code:
#E
import bisect

s = input()
t = input()
lens = len(s)

s_n = {}
for i in range(len(s)):
    if s[i] in s_n:
        s_n[s[i]].append(i+1)
    else:
        s_n[s[i]] = [i+1]
        
#print(s_n)

d1=0
d2=0
for i in t:
    if not(i in s_n):
        print(-1)
        break
    else:
        if d1 >= s_n[i][-1]:
            d1 = s_n[i][0]
            d2 += lens
        else:
            index = bisect.bisect(s_n[i],d1)
            d1 = s_n[i][index]
else:
    print(d1+d2)
Buggy code:
s = input()
t = list(input())

index = 0
rep = 0
length = len(s)
ans = 0
for tar in t:
    old_index = index
    kari_index = s[old_index+1:].find(tar)

    if kari_index == -1:
        tmp = s.find(tar)
        if  tmp == -1:
            ans = -1
        else:
            rep += 1
            index = tmp
    else:
        index += kari_index + 1

ans = rep * len(s) + index + 1
print(ans)

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

index = -1
rep = 0
length = len(s)
ans = 0
for tar in t:
    if index+1 == length:
        rep += 1
        index = -1
        
    kari_index = s[index+1:].find(tar)

    if kari_index == -1:
        tmp = s.find(tar)
        if  tmp == -1:
            ans = -1
        else:
            rep += 1
            index = tmp
    else:
        index += kari_index + 1

if ans != -1:
    ans = rep * len(s) + index + 1
print(ans)
Buggy code:
s = input()
t = input()
ans = 0

for ch in t:
    if ch not in s:
        print('-1')

x = s
for ch in t:
    p = x.find(ch) + 1
    if p == 0:
        ans += len(x)
        x = s
    ans += p
    if p >= len(x) :
        p = 0
    x = x[p:]

print(ans)



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


for ch in t:
    if ch not in s:
        print('-1')
        exit()

x = s
for ch in t:
    p = x.find(ch) + 1
    if p == 0:
        ans += len(x)
        p = s.find(ch) + 1
        x = s
    ans += p
    
    x = x[p:]

print(ans)
Buggy code:
import sys
from collections import defaultdict

def bs(arr, val):
	l, h = 0, len(arr)-1
	ans = -1
	while l <= h:
		m = l + h >> 1
		if arr[m] > val:
			ans = m
			h = m - 1
		else:
			l = m + 1
	return ans

s = sys.stdin.readline()
n = len(s)

t = sys.stdin.readline()

occurence = defaultdict(list)
for i in range(n):
	occurence[s[i]].append(i+1)

folds = 0
cur_ptr = 0
for ele in t:
	indexes = occurence[ele]
	idx = bs(indexes, cur_ptr)
	if idx != -1: 
		cur_ptr = indexes[idx]
	else:
		folds += 1
		cur_ptr = indexes[0]
ans = folds*(n) + cur_ptr
print(ans)

Fixed code:
import sys
from collections import defaultdict

def bs(arr, val):
	l, h = 0, len(arr)-1
	ans = -1
	while l <= h:
		m = l + h >> 1
		if arr[m] > val:
			ans = m
			h = m - 1
		else:
			l = m + 1
	return ans

s = sys.stdin.readline().strip()
n = len(s)

t = sys.stdin.readline().strip()

occurence = defaultdict(list)
for i in range(n):
	occurence[s[i]].append(i+1)

def solve():
	folds = 0
	cur_ptr = 0

	for ele in t:
		indexes = occurence[ele]
		if not indexes: return -1

		idx = bs(indexes, cur_ptr)
		if idx != -1: 
			cur_ptr = indexes[idx]
		else:
			folds += 1
			cur_ptr = indexes[0]
		# print(ele, cur_ptr, folds)
	ans = folds*(n) + cur_ptr
	return ans

print(solve())
Buggy code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

s = input()
n = len(s)
t = input()
s*=2

ans = 0
for c in t:
    d = s[ans%n:].find(c)
    ans += d
print(ans+1)

Fixed code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

s = input()
n = len(s)
t = input()
s*=2

ans = 0
for c in t:
    d = s[ans%n:].find(c)
    if d == -1:
        print(-1)
        exit()
    ans += d+1
print(ans)
Buggy code:
from bisect import bisect_right

s = input()
t = input()

char_list = [[] for i in range(26)]

for i in range(2*len(s)):
        if i<len(s):
                char_list[ord(s[i])-97].append(i)
        else:
                char_list[ord(s[i-len(s)])-97].append(i)

INF = 10**27

for i in range(26):
        char_list[i].append(INF)

next_list = [[char_list[j][bisect_right(char_list[j],i)] for j in range(26)] for i in range(len(s))]

flag = False
for i in range(len(s)):
        if t[0]==s[i]:
                flag=True
                x = i
                y = i
                break
if flag:
        flag2 = True
        for i ina range(1,len(t)):
                n = next_list[x][ord(t[i])-97]
                y += n - x
                x = n%(len(s))
                if y >= INF:
                        flag2=False
                        break
        if flag2:
                print(y+1)
        else:
                print(-1)
else:
        print(-1)

Fixed code:
from bisect import bisect_right

s = input()
t = input()

char_list = [[] for i in range(26)]

for i in range(2*len(s)):
        if i<len(s):
                char_list[ord(s[i])-97].append(i)
        else:
                char_list[ord(s[i-len(s)])-97].append(i)

INF = 10**27

for i in range(26):
        char_list[i].append(INF)

next_list = [[char_list[j][bisect_right(char_list[j],i)] for j in range(26)] for i in range(len(s))]

flag = False
for i in range(len(s)):
        if t[0]==s[i]:
                flag=True
                x = i
                y = i
                break
if flag:
        flag2 = True
        for i in range(1,len(t)):
                n = next_list[x][ord(t[i])-97]
                y += n - x
                x = n%(len(s))
                if y >= INF:
                        flag2=False
                        break
        if flag2:
                print(y+1)
        else:
                print(-1)
else:
        print(-1)
Buggy code:
import bisect
s = input()*2
lens = len(s)//2
t = input()
appear = {ch: [] for ch in 'abcdefghijklmnopqrstuvwxyz'}
cnt = -1
bis = bisect.bisect_right
ans = 0

for i, ch in enumerate(s):
  appear[ch].append(i)
  
for j in t:
  if appear[j]:
    idx = bis(appear[j],cnt)
    ans += appear[j][idx] - cnt
    cnt = appear[j][idx]%lens
    print(cnt)
  else:
    print(-1)
    exit()
print(ans)

Fixed code:
import bisect
s = input()*2
lens = len(s)//2
t = input()
appear = {ch: [] for ch in 'abcdefghijklmnopqrstuvwxyz'}
cnt = -1
bis = bisect.bisect_right
ans = 0

for i, ch in enumerate(s):
  appear[ch].append(i)
  
for j in t:
  if appear[j]:
    idx = bis(appear[j],cnt)
    ans += appear[j][idx] - cnt
    cnt = appear[j][idx]%lens
  else:
    print(-1)
    exit()
print(ans)
Buggy code:
from collections import defaultdict
import bisect
s = input()
t = input()
#%%
ls = len(s)-1
cnt = 0
num = 0
di = defaultdict(list)
for ind,i in enumerate(s):
    di[i].append(ind)

for i in t:
    num = cnt%(ls+1)
    if(i not in di):
        print(-1)
        break
    if(max(di[i])<=num):
        cnt += ls-num+min(di[i])
    else:
        x = bisect.bisect_left(di[i],num)
        cnt += di[i][x]-num+1
    print(i,cnt)

else:
    print(cnt+1)


Fixed code:
from collections import defaultdict
import bisect

s = input()
t = input()
#%%
ls = len(s)-1
cnt = 0
num = -1
di = defaultdict(list)
for ind,i in enumerate(s):
    di[i].append(ind)

for i in t:
    if(i not in di):
        print(-1)
        break
    if(di[i][-1]<=num):
        cnt += ls-num+di[i][0]+1
    else:
        x = bisect.bisect_left(di[i],num+1)
        if(num==-1):
            cnt += di[i][x]
        else:
            cnt += di[i][x]-num
    num = cnt%(ls+1)
    #print(i,cnt)

else:
    print(cnt+1)
Buggy code:
import bisect

s = input()
t = input()
T = set(t)
s_num = len(s)
t_num = len(T)

# make a dictonary whose keys are the alphabets in t
dict = {}
appearing_times = {}

# when each alphabet in t appears in s
for w in T:
    dict[w] = []
    appearing_times[w] = 0
    index = -1
    while True:
        index = s.find(w, index + 1)
        if index == -1 and appearing_times[w] == 0:
            print(-1)
            exit()
        elif index == -1:
            break
        else:
            dict[w].append(index)
            appearing_times[w] += 1

# how many times s is repeated
count = 0
# 0 <= length <= s_num - 1
s_index = 0
check = 0

for w in t:
    idx = bisect.bisect_left(dict[w], s_index)
    if check == 'done' and s_index == dict[w][idx]:
        idx = idx + 1

    if idx < appearing_times[w]:
        s_index = dict[w][idx]
    else:
        count += 1
        s_index = dict[w][0]

    check = 'done'
print(count * s_num + s_index + 1)


Fixed code:
import bisect

s = input()
t = input()
T = set(t)
s_num = len(s)
t_num = len(T)

# make a dictonary whose keys are the alphabets in t
dict = {}
appearing_times = {}

# when each alphabet in t appears in s
for w in T:
    dict[w] = []
    appearing_times[w] = 0
    index = -1
    while True:
        index = s.find(w, index + 1)
        if index == -1 and appearing_times[w] == 0:
            print(-1)
            exit()
        elif index == -1:
            break
        else:
            dict[w].append(index)
            appearing_times[w] += 1

# how many times s is repeated
count = 0
# 0 <= length <= s_num - 1
s_index = 0
w_pre = 'none'

for w in t:
    idx = bisect.bisect_left(dict[w], s_index)
    if w == w_pre:
        idx += 1
        if idx == appearing_times[w]:
            count += 1
            idx = 0

    if idx < appearing_times[w]:
        s_index = dict[w][idx]
    else:
        count += 1
        s_index = dict[w][0]
    w_pre = w

print(count * s_num + s_index + 1)
Buggy code:
s = input()
t = input()
s_ = s[:]
cnt = 0
tlen = len(t)
ans = 0
flag = True

while cnt < tlen:
    
    num = s_.find(t[cnt])
    
    if num != -1:
        ans += (num+1)
        s_ = s_[(num+1):]
        cnt += 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[:]
cnt = 0
tlen = len(t)
ans = 0
flag = True

while cnt < tlen:
    
    num = s_.find(t[cnt])
    
    if num != -1:
        ans += (num+1)
        s_ = s_[(num+1):]
        cnt += 1
        flag = False
    else:
        if flag:
            print(-1)
            exit(0)
        ans += len(s_)
        s_ = s[:]
        flag = True
        
print(ans)
Buggy code:
s = input()
t = input()
n = len(s)
nxt = [list() for x in range(n + 1)]
nxt[n] = [n for x in range(26)]
print(*nxt[n])
for i in range (n - 1, -1, -1):
    for v in nxt[i + 1]:
        nxt[i].append(v)
    nxt[i][ord(s[i]) - ord('a')] = i;
ans = 0
cur = 0
for i in range (len(t)):
    c = ord(t[i]) - ord('a')
    if (nxt[0][c] == n) :
        ans = 0
        cur = -1
        break
    if (nxt[cur][c] == n) :
        print("wtf")
        ans += 1
        cur = 0
    cur = nxt[cur][c] + 1
print(ans * n + cur)

Fixed code:
s = input()
t = input()
n = len(s)
nxt = [list() for x in range(n + 1)]
nxt[n] = [n for x in range(26)]
for i in range (n - 1, -1, -1):
    for v in nxt[i + 1]:
        nxt[i].append(v)
    nxt[i][ord(s[i]) - ord('a')] = i;
ans = 0
cur = 0
for i in range (len(t)):
    c = ord(t[i]) - ord('a')
    if (nxt[0][c] == n) :
        ans = 0
        cur = -1
        break
    if (nxt[cur][c] == n) :
        ans += 1
        cur = 0
    cur = nxt[cur][c] + 1
print(ans * n + cur)
Buggy code:
s,t=([chr(c)-chr('a') for c in input()]for _ in'xx')
if set(t)-set(s):
  print(-1)
else:
  u=s+s
  nxt = [None for _ in range(len(u)+1)]
  nxt[len(u)]=[0 for _ in range(26)]
  for i in range(len(u)-1,-1,-1):
    nxt[i] = [v+1 for v in nxt[i+1]]
    nxt[i][u[i]]=0
  p = 0
  for c in t:
    p += nxt[p%len(s)][c]+1
  print(p)

Fixed code:
s,t=([ord(c)-ord('a') for c in input()]for _ in'xx')
if set(t)-set(s):
  print(-1)
else:
  u=s+s
  nxt = [None for _ in range(len(u)+1)]
  nxt[len(u)]=[0 for _ in range(26)]
  for i in range(len(u)-1,-1,-1):
    nxt[i] = [v+1 for v in nxt[i+1]]
    nxt[i][u[i]]=0
  p = 0
  for c in t:
    p += nxt[p%len(s)][c]+1
  print(p)
Buggy code:
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations
import sys
import bisect
import string
sys.setrecursionlimit(10**6)
def SI():
    return input().split()
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]
YN=['Yes','No']
mo=10**9+7
alp=string.ascii_lowercase
d_al=dict([(i,j) for j,i in enumerate(alp)])

s=[d_al[i] for i in input()]
t=[d_al[i] for i in input()]
Ss=set(s)
St=set(t)
ns=len(s)
nt=len(t)
ss=s*2
#print(ss)

g=[{} for _ in range(ns)]
pos=[-1]*26
for i in range(ns*2)[::-1]:
    for j in Ss:
        if i<ns:
            g[i][j]=pos[j]-i
    pos[ss[i]]=i
#for i in range(len(g)):
#    print(g[i])

if len(St-Ss)!=0:
    ans=-1
else:
    ans=0
    cur=0
    for i in range(nt):
        ans+=g[cur][t[i]]
        cur+=g[cur][t[i]]
        cur%=ns
print(ans)#,loop,has)


Fixed code:
from heapq import heappush, heappop
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations
import sys
import bisect
import string
sys.setrecursionlimit(10**6)
def SI():
    return input().split()
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]
YN=['Yes','No']
mo=10**9+7
alp=string.ascii_lowercase
d_al=dict([(i,j) for j,i in enumerate(alp)])

s=[d_al[i] for i in input()]
t=[d_al[i] for i in input()]
Ss=set(s)
St=set(t)
ns=len(s)
nt=len(t)
ss=s*2
#print(ss)

g=[{} for _ in range(ns)]
pos=[-1]*26
for i in range(ns*2)[::-1]:
    for j in Ss:
        if i<ns:
            g[i][j]=pos[j]-i
    pos[ss[i]]=i
#for i in range(len(g)):
#    print(g[i])

if len(St-Ss)!=0:
    ans=-1
else:
    cur=0
    cur+=g[-1][t[0]]-1
    #print(cur)
    for i in range(1,nt):
        #print(cur,s[cur%ns],'tg=',t[i],g[cur%ns][t[i]])
        cur+=g[cur%ns][t[i]]
    ans=cur+1
print(ans)
Buggy code:
s = input()
t = input()

for i in range(len(t)):
  if not t[i] in s:
    print(-1)
    break
else:
  j = 0
  for i in rane(len(t)):
    j = s[j:].find(t[i])
    if j == -1:
      counter += 1
      j = s.find(t[i])
  print(counter * len(s) + j + 1)

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

for i in range(len(t)):
  if not t[i] in s:
    print(-1)
    break
else:
  j = -1
  counter = 0
  for i in range(len(t)):
    if t[i] in s[j+1:]:
      j = s[j+1:].find(t[i]) + j + 1
    else:
      counter += 1
      j = s.find(t[i])
  print(counter * len(s) + j + 1)
Buggy code:
Copy
Copy
from bisect import bisect_left
 
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
 
 
def main(s, t):
    positions = {char: [] for char in ALPHABET}
    for i, char in enumerate(s):
        positions[char].append(i)
 
    current = 0
    answer = 0
    for char in t:
        pos_num = len(positions[char])
        if pos_num < 1:
            return -1
        idx = bisect_left(positions[char], current)
        if idx < pos_num:
            current = positions[char][idx] + 1
        else:
            answer += len(s)
            idx = bisect_left(positions[char], 0)
            current = positions[char][idx] + 1
 
    answer += current
    return answer
 
 
s = input()
t = input()
print(main(s, t))

Fixed code:
from bisect import bisect_left

ALPHABET = "abcdefghijklmnopqrstuvwxyz"


def main(s, t):
    positions = {char: [] for char in ALPHABET}
    for i, char in enumerate(s):
        positions[char].append(i)

    current = 0
    answer = 0
    for char in t:
        pos_num = len(positions[char])
        if pos_num < 1:
            return -1
        idx = bisect_left(positions[char], current)
        if idx < pos_num:
            current = positions[char][idx] + 1
        else:
            answer += len(s)
            idx = bisect_left(positions[char], 0)
            current = positions[char][idx] + 1

    answer += current
    return answer


s = input()
t = input()
print(main(s, t))
Buggy code:
# -*- coding: utf-8 -*-


def main():
    s = input().splitlines()[0]
    t = input()

    Slen = len(s)

    LookupTable = [[] for i in range(26)]
    for i, c in enumerate(s):
        v = ord(c) - ord('a')
        LookupTable[v].append(i)
    for i in range(26):
        LookupTable[i].append(Slen)
        
    IndexTable = [0 for i in range(26)]
    NextTable = []
    for i, c in enumerate(s):
        v = ord(c) - ord('a')
        NextTable.append( [LookupTable[k][IndexTable[k]] for k in range(26)] )
        if LookupTable[v][IndexTable[v]] >= i:
            IndexTable[v] += 1
        if LookupTable[v][IndexTable[v]] == Slen:
            IndexTable[v] = 0
    NextTable.append( [LookupTable[k][IndexTable[k]] for k in range(26)] )
        
    Index = 0
    Loop = 0
    for i, c in enumerate(t):
        v = ord(c) - ord('a')
        if NextTable[Index][v] >= Index:
            Loop += 1
        Index = NextTable[Index][v]

    if Loop == Slen:
        print(-1)
    else:
        print(Loop*Slen+Index+1)


if __name__ == "__main__":
    main()


Fixed code:
# -*- coding: utf-8 -*-


def main():
    s = input().splitlines()[0]
    t = input().splitlines()[0]

    Slen = len(s)

    LookupTable = [[] for i in range(26)]
    for i, c in enumerate(s):
        v = ord(c) - ord('a')
        LookupTable[v].append(i)
    for i in range(26):
        LookupTable[i].append(-1)
        
    IndexTable = [0 for i in range(26)]
    NextTable = []
    for i, c in enumerate(s):
        v = ord(c) - ord('a')
        NextTable.append( [LookupTable[k][IndexTable[k]] for k in range(26)] )
        IndexTable[v] += 1
        if LookupTable[v][IndexTable[v]] < 0:
            IndexTable[v] = 0

    Index = 0
    Loop = 0
    for i, c in enumerate(t):
        v = ord(c) - ord('a')
        if NextTable[Index][v] < Index:
            Loop += 1
        Index = NextTable[Index][v]
        if Index < 0:
            print(-1)
            return
        Index += 1
        if Index >= Slen:
            Index = 0
            Loop += 1

    print(Loop*Slen+Index)


if __name__ == "__main__":
    main()
Buggy code:
#!/usr/bin/env python3
#ABC138 E

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = input()
t = input()
n = len(s)
m = len(t)
f = [False]*26
f2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(n):
    f[alpha.index(s[i])] = True
for i in range(m):
    f2[alpha.index(t[i])] = True
for i in range(26):
    if not f[i] and f2[i]:
        print(-1)
        quit()
x = []
inde = s.index(t[0])
x.append(inde+1)
if m > 1:
    x.append(n-inde-1)
for i in range(1,m):
    inde = s.index(t[i])
    preinde = s[i:].index(t[i])
    if inde > preinde:
        x.pop()
        x.append(inde-preinde)
        if i != m-1:
            x.append(n-inde-1)
    else:
        x.append(inde+1)
        if i != m-1:
            x.append(n-inde-1)
print(sum(x))


Fixed code:
#!/usr/bin/env python3
#ABC138 E

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

s = input()
t = input()
n = len(s)
m = len(t)
f = [False]*26
f2 = [False]*26
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(n):
    f[alpha.index(s[i])] = True
for i in range(m):
    f2[alpha.index(t[i])] = True
for i in range(26):
    if not f[i] and f2[i]:
        print(-1)
        quit()
ans = 0
pre = s.index(t[0])
ans += pre+1
for i in range(1,m):
    s = s[pre+1:] + s[:pre+1]
    inde = s.index(t[i])
    ans += inde+1
    pre = inde
print(ans)
Buggy code:
s = input()
t = input()

lst = [[] for _ in range(30)]
ch = 'abcdefghijklmnopqrstuvwxyz'
for k, i in enumerate(s+s):
    lst[ch.find(i)].append(k)

len_s = len(s)

count = 0
index = -1
for i in t:
    if lst[ch.find(i)] == []:
        print(-1)
        break
    next_index = lst[ch.find(i)][bisect.bisect_right(lst[ch.find(i)], index)]
    count += next_index - index
    if next_index >= len_s:
        next_index -= len_s
    index = next_index
else:
    print(count)

Fixed code:
import bisect

s = input()
t = input()

lst = [[] for _ in range(30)]
ch = 'abcdefghijklmnopqrstuvwxyz'
for k, i in enumerate(s+s):
    lst[ch.find(i)].append(k)

len_s = len(s)

count = 0
index = -1
for i in t:
    if lst[ch.find(i)] == []:
        print(-1)
        break
    next_index = lst[ch.find(i)][bisect.bisect_right(lst[ch.find(i)], index)]
    count += next_index - index
    if next_index >= len_s:
        next_index -= len_s
    index = next_index
else:
    print(count)
Buggy code:
import bisect
s=input()
t=input()
dic={}
for v in s:
    dic[v]=True

for u in s:
    if not dic[u]:
        print(-1)
        exit()

nex={}
for i, v in enumerate(s+s):
    if not v in nex:
        nex[v]=[i]
    else:
        nex[v].append(i)
#print(s)
#print(t)
#print(nex)
sl=len(s)
tl=len(t)
def func(st, tn, su):
    #print(st, tn, su)
    if tn == tl:
        return su 
    ind=bisect.bisect_left(nex[t[tn]] ,st)
    nm=(nex[t[tn]][ind]-st)
    return func((st+nm)%sl, tn+1, su+nm)


res=func(-1, 0, 0)

print(res)

Fixed code:
import bisect
import sys

sys.setrecursionlimit(1000000)

s=input()
t=input()
dic={}
for v in s:
    dic[v]=True

for u in t:
    if not u in dic:
        print(-1)
        exit()

nex={}
for i, v in enumerate(s+s):
    if not v in nex:
        nex[v]=[i]
    else:
        nex[v].append(i)
#print(s)
#print(t)
#print(nex)
sl=len(s)
tl=len(t)
def func(st, tn, su):
    if tn == tl:
        return su
    #print(st, tn, su, t[tn])
    ind=bisect.bisect_left(nex[t[tn]] ,st+1)
    nm=(nex[t[tn]][ind]-st)
    return func((st+nm)%sl, tn+1, su+nm)


res=func(-1, 0, 0)

print(res)