imashutosh51

Palindrome Partition

Mar 20th, 2026
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.44 KB | None | 0 0
  1. from math import inf
  2. def fun(s,i,j,dp):
  3.     if i>=j:
  4.         return 0
  5.     if dp[i][j]!=-1:
  6.         return dp[i][j]
  7.     if s[i:j+1] == s[i:j+1][::-1]:
  8.         return 0
  9.     temp=inf
  10.     for k in range(i,j):
  11.         temp=min(temp, 1+fun(s,i,k,dp)+ fun(s,k+1,j,dp))
  12.     dp[i][j]=temp
  13.     return dp[i][j]
  14.  
  15. class Solution:
  16.     def palPartition(self, s):
  17.         dp=[[-1]*len(s) for _ in range(len(s))]
  18.         return fun(s,0,len(s)-1,dp)
Advertisement
Add Comment
Please, Sign In to add comment