CORUS problem(MAY CHALLENGE 2020), not understanding why is it showing TLE errors

t=int(input())
i=0
j=0
for i in range(t):
x, y = [int(x) for x in input().split()]
s=input()
for i in range(y):
pq=0
center=int(input())
for i in range(x):
c=1
for j in range(i+1,x):
if(s[i]==s[j] and s[i]!=’ '):
c=c+1
s= s[:j] + ‘0’ + s[j+1:]
if(c>1 and s[i]!=‘0’):
if(c>=center):
pq=pq+c-center
else:
pq=pq+0
print(pq)

1 Like

A sample case for which your code fails:
1
5 1
abcde
0

Your o/p:
0
Expected o/p:
5

2 Likes

You can try this approach.

def fun(h,c):
    s=0
    for i in range(26):
        if h[i]>c:
            s+=(h[i]-c)
    return s
for _ in range (int(input())):
    n,q=map(int,input().split())
    a=input()
    h=[0]*26
    for i in a:
        h[ord(i)-ord('a')]+=1
    for i in range(q):
        c=int(input())
        print(fun(h,c))

i made the correction still its showing error