Iam_Sandeep

Palindrome Partitioning DP

Apr 28th, 2022 (edited)
1,928
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.66 KB | None | 0 0
  1. #User function Template for python3
  2. #T.C:O(n**2)
  3. #User function Template for python3
  4. '''
  5. Idea
  6.  
  7. Let isPalindrome(l, r) be True if string s[l...r] is palindrome else False.
  8. Let dp(i) denote the minimum number of palindrome substrings of string s[i..n-1].
  9. The answer is dp(0) - 1 which is number minimum cuts need to be made on string s[0..n-1].
  10. Complexity
  11.  
  12. Time: O(N^2)
  13. Time complexity for isPalindrome(l, r) is O(N^2)
  14. Time complexity for dp(i) is O(N^2)
  15. So total time complexity is O(N^2).
  16. Space: O(N^2)
  17.  
  18. '''
  19. import sys
  20. sys.setrecursionlimit(10**9)
  21. from functools import lru_cache
  22. class Solution:
  23.     def palindromicPartition(self, s):
  24.         @lru_cache()
  25.         def palindrome(i,j):
  26.             if i==j:return True
  27.             if i+1==j:
  28.                 return s[i]==s[j]
  29.             if s[i]==s[j]:
  30.                 return palindrome(i+1,j-1)
  31.             return False
  32.         n=len(s)
  33.         @lru_cache()
  34.         def dp(i):
  35.             if i==n:return 0
  36.             ans=float('inf')
  37.             for k in range(i,n):
  38.                 if palindrome(i,k):
  39.                     ans=min(ans,1+dp(k+1))
  40.             return ans
  41.         return dp(0)-1# we have to sub -1 because take e.g. 'aaa'. Reason at base case are also we are adding extra parition. That we have to remove
  42.                    
  43. #T.C.:O(n**3)
  44. class Solution:
  45.     def palindromicPartition(self, s):
  46.         n=len(s)
  47.         dp=[[-1]*(1+n) for i in range(1+n)]
  48.         bools=[[None]*(1+n) for i in range(1+n)]
  49.         def ispalindrome(i,j):
  50.             l,r=i,j
  51.             if bools[i][j]!=None:return bools[i][j]
  52.             while l<=r:
  53.                 if s[l]!=s[r]:
  54.                     bools[i][j]=False
  55.                     return False
  56.                 l,r=l+1,r-1
  57.             bools[i][j]=True
  58.             return True
  59.         def find(i,j):
  60.             if i>=j:return 0
  61.             if dp[i][j]!=-1:return dp[i][j]
  62.             ans=float('inf')
  63.             if ispalindrome(i,j):return 0
  64.             '''    
  65.            An Optimization: We will make the partition only if the string till the partition
  66.         (till Kth position) is a valid palindrome. Because the question states that all
  67.         partition must be a valid palindrome. If we dont check this, we will have to
  68.         perform recursion on the left subproblem too (solve(str, i, k)) and we will waste
  69.         a lot of time on subproblems that is not required. Without this the code will give
  70.         correct answer but TLE on big test cases.
  71.             '''
  72.             for k in range(i,j):
  73.                 if ispalindrome(i,k):
  74.                     ans=min(ans,1+find(k+1,j))
  75.             dp[i][j]=ans
  76.             return ans
  77.         return find(0,n-1)
  78.  
Add Comment
Please, Sign In to add comment