查找第一个字符串中出现在 Python 中第二个字符串中最小索引处的字符
假设我们有一个字符串str和另一个字符串patt,我们必须找到确定patt中出现在str的最小索引处的字符。如果str1中不存在字符patt1,则返回-1。
因此,如果输入类似于str="helloworld"和patt="wor",那么输出将是'o',因为'o'出现在str中的最小索引处
在线示例
让我们看看以下实现以获得更好的理解-
def get_min_index_char(Str, patt):
minimum_index = 10**9
for i in range(len(patt)):
for j in range(len(Str)):
if (patt[i] == Str[j] and j < minimum_index):
minimum_index = j
break
if (minimum_index != 10**9):
return Str[minimum_index]
else:
return -1
Str = "helloworld"
patt = "wor"
print(get_min_index_char(Str, patt))输入
"helloworld", "wor"输出结果
o
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短