DeepRest

Palindrome Partitioning

Jan 5th, 2022 (edited)
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. '''
  2. Bottom up DP:
  3. in most cases some subproblems are never used
  4. dp[i] = lists of all valid partitions of suffixes starting from i
  5. dp[i] = palindromes starting from i of len p + partitions in  dp[i+p] for all 1<=p<=N-i
  6. Base case dp[N] = [[]]
  7. Also palindrome prefixes of s[i+1:] can be obtained from palindrome prefixes of s[i:]
  8. '''
  9. class Solution:
  10.     def partition(self, s: str) -> List[List[str]]:
  11.         n = len(s)
  12.         s += '/'
  13.         palindromes = []
  14.         partitions = {n: [[]]}
  15.         for i in range(n-1, -1, -1):
  16.             temp = [s[i]]
  17.             for p in palindromes:
  18.                 pos = i+len(p)+1
  19.                 if s[i] == s[pos]:
  20.                     temp.append(s[i:pos+1])  
  21.            
  22.             partitions[i] = []
  23.             for p in temp:
  24.                 pos = i+len(p)
  25.                 for q in partitions[pos]:
  26.                     partitions[i].append([p] + q)
  27.             palindromes = [''] + temp
  28.        
  29.         return partitions[0]
  30.  
  31. '''DP for palindrome check + bactracking:
  32. dp[l][r] = isPalindrome(s[l...r])
  33. dp[l][r] = True. for r == l
  34.         = s[l] == s[r]. for r == l+1
  35.         = dp[l+1][r-1] && (s[l] == s[r]) otherwise
  36. '''
  37. class Solution:
  38.     def partition(self, s: str) -> List[List[str]]:
  39.         N = len(s)
  40.         s += '/'
  41.         dp = [[False]*(N+1) for _ in range(N)]
  42.         for L in range(N-1, -1, -1):
  43.             dp[L][L] = True
  44.             dp[L][L+1] = (s[L] == s[L+1])
  45.             for R in range(L+2, N):
  46.                 dp[L][R] = (s[L] == s[R]) and dp[L+1][R-1]
  47.         res = []
  48.        
  49.         def backtrack(curr, partition):
  50.             if curr == N:
  51.                 res.append(partition[:])
  52.            
  53.             for right in range(curr, N):
  54.                 if dp[curr][right]:
  55.                     partition.append(s[curr:right+1])
  56.                     backtrack(right+1, partition)
  57.                     partition.pop()  
  58.         backtrack(0, [])
  59.        
  60.         return res
  61.  
  62. '''
  63. Bactracking with memoized palindrome check
  64. '''
  65. class Solution:
  66.     def partition(self, s: str) -> List[List[str]]:
  67.         N = len(s)
  68.         res = []
  69.        
  70.         @cache
  71.         def isPal(s, curr, right):
  72.             while curr<=right and s[curr] == s[right]:
  73.                 curr += 1
  74.                 right -= 1
  75.             return curr > right
  76.            
  77.        
  78.         def backtrack(curr, partition):
  79.             if curr == N:
  80.                 res.append(partition[:])
  81.            
  82.             for right in range(curr, N):
  83.                 if isPal(s, curr, right):
  84.                     partition.append(s[curr:right+1])
  85.                     backtrack(right+1, partition)
  86.                     partition.pop()  
  87.         backtrack(0, [])
  88.        
  89.         return res
Advertisement
Add Comment
Please, Sign In to add comment