我正在编写一个代码,该代码返回特定(用户输入)索引之前的char计数。大量输入的时间已用完。任何人都可以帮助我降低其时间复杂度。这是代码。
le=int(input()) #len string
st=input() #string
tot=int(input()) # total cases
for i in range(tot):
pos=int(input()) #index of char
sub=st[0:pos]
cou=sub.count(sub[pos-1])
print(cou-1)