AcWing 778. 字符串最大跨距 python(忘记rfind了)
原题链接
困难
作者:
洒水水
,
2024-03-18 10:01:10
,
所有人可见
,
阅读 6
def find_last_index(S,s): #找出原字符串中最后一个子字符串的索引下标,没找到则返回-1
star_index = index = 0
while True:
index = S.find(s,star_index)
if index == -1:
break
else:
star_index = index + 1
return star_index - 1
S,s1,s2 = input().split(',')
s1_index = S.find(s1)
s2_index = find_last_index(S,s2)
if s1_index != -1 and s2_index != -1 and s2_index >= (s1_index + len(s1)): #满足三个条件
print(s2_index - s1_index - len(s1))
else:
print(-1)