Why tle? please help to optimize the code


#1

Comment body goes here.
def minCut(self, A):
s=A
m=len(s)
t=[[-1 for i in range(m+1)]for j in range(m+1)]
r=self.solve(s,0,m,t)
return r
def solve(self,s,i,j,t):
if i>=j:
return 0
if s[i:j+1]==s[i:j+1][::-1]:
return 0
if t[i][j]!=-1:
return t[i][j]
mn=float(‘inf’)
for k in range(i,j-1):
if t[i][k]!=-1:
left=t[i][k]
else:
left=self.solve(s,i,k,t)
t[i][k]=left
if t[k+1][j]!=-1:
right=t[k+1][j]
else:
right=self.solve(s,k+1,j,t)
t[k+1][j]=right
temp=1+left+right
mn=min(mn,temp)
t[i][j]=mn
return mnPreformatted text