Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import inf
- def fun(s,i,j,dp):
- if i>=j:
- return 0
- if dp[i][j]!=-1:
- return dp[i][j]
- if s[i:j+1] == s[i:j+1][::-1]:
- return 0
- temp=inf
- for k in range(i,j):
- temp=min(temp, 1+fun(s,i,k,dp)+ fun(s,k+1,j,dp))
- dp[i][j]=temp
- return dp[i][j]
- class Solution:
- def palPartition(self, s):
- dp=[[-1]*len(s) for _ in range(len(s))]
- return fun(s,0,len(s)-1,dp)
Advertisement
Add Comment
Please, Sign In to add comment