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.