Palindrome Partitioning
LeetCode 131 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Backtracking over palindromic prefixes; a dp table can remember which substrings are palindromes.
நான் Palindrome Partitioning (LeetCode 131) ஐ இப்படித் தீர்த்தேன் — Partitioning பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
Cut the string from left to right. At each position try every end where s[start..end] is a palindrome, take it as the next piece, and recurse on the rest. Arriving at the end of the string means the current list of pieces is a valid partition.
Approach
- 1
backtrack(start, parts): if start == len(s), save a copy of parts.
- 2
For each end ≥ start, check s[start..end] is a palindrome.
- 3
Append it, recurse from end + 1, then pop.
- 4
Second version marks dp[start][end] using the ends-match and inner-range rule instead of scanning.
நான் எப்படித் தீர்த்தேன் · முதல் யோசனையிலிருந்து இறுதித் தீர்வு வரை
Check each piece with is_palindrome.
dp marks palindromic ranges.
Backtracking + two-pointer checkPythonO(n·2ⁿ) time
class Solution:
def partition(self, s: str) -> List[List[str]]:
res=[]
def is_palindrome(start,end):
while start<end:
if s[start]!=s[end]:
return False
start+=1
end-=1
return True
def backtrack(start, cur_partitions):
if start == len(s):
res.append(cur_partitions[:])
return
for end in range(start,len(s)):
if is_palindrome(start,end):
cur_partitions.append(s[start:end+1])
backtrack(end+1,cur_partitions)
cur_partitions.pop()
backtrack(0,[])
return resBacktracking + palindrome tablePythonO(n·2ⁿ) time · O(n²) space
class Solution:
def partition(self, s: str) -> List[List[str]]:
res=[]
n = len(s)
dp=[[False]*n for _ in range(n)]
def backtrack(start, cur_partitions):
if start == len(s):
res.append(cur_partitions[:])
return
for end in range(start,len(s)):
if s[start]==s[end] and ((end-start)<=2 or dp[start+1][end-1]==True):
dp[start][end]=True
cur_partitions.append(s[start:end+1])
backtrack(end+1,cur_partitions)
cur_partitions.pop()
backtrack(0,[])
return resPalindrome Partitioning — அடிக்கடி கேட்கப்படும் கேள்விகள்
Palindrome Partitioning பின்னாலுள்ள intuition என்ன?
Cut the string from left to right. At each position try every end where s[start..end] is a palindrome, take it as the next piece, and recurse on the rest. Arriving at the end of the string means the current list of pieces is a valid partition.
Palindrome Partitioning (LeetCode 131) ஐ எப்படித் தீர்ப்பது?
1. backtrack(start, parts): if start == len(s), save a copy of parts. 2. For each end ≥ start, check s[start..end] is a palindrome. 3. Append it, recurse from end + 1, then pop. 4. Second version marks dp[start][end] using the ends-match and inner-range rule instead of scanning.
Palindrome Partitioning இன் time மற்றும் space complexity என்ன?
Time: O(n · 2ⁿ). Space: O(n²) for the table. (Accepted approach: Backtracking + palindrome table.)
Palindrome Partitioning இல் பொதுவான தவறுகள் என்ன?
Append a copy (parts[:]) — the list keeps changing. The inner check dp[start+1][end-1] is only valid for ranges already marked.
Palindrome Partitioning ஐத் தீர்க்க ஒன்றுக்கு மேற்பட்ட வழிகள் உள்ளனவா?
Backtracking + two-pointer check (O(n·2ⁿ) time): Check each piece with is_palindrome. Backtracking + palindrome table (O(n·2ⁿ) time · O(n²) space): dp marks palindromic ranges.