Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 131 · Partitioning

Palindrome Partitioning

LeetCode 131 · మధ్యస్థం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

Backtracking over palindromic prefixes; a dp table can remember which substrings are palindromes.

మధ్యస్థంStringDynamic ProgrammingBacktracking

నేను 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. 1

    backtrack(start, parts): if start == len(s), save a copy of parts.

  2. 2

    For each end ≥ start, check s[start..end] is a palindrome.

  3. 3

    Append it, recurse from end + 1, then pop.

  4. 4

    Second version marks dp[start][end] using the ends-match and inner-range rule instead of scanning.

నేను ఎలా పరిష్కరించాను · మొదటి ఆలోచన నుంచి చివరి పరిష్కారం వరకు

1
Backtracking + two-pointer checkO(n·2ⁿ) time

Check each piece with is_palindrome.

2
Backtracking + palindrome tableAcceptedO(n·2ⁿ) time · O(n²) space

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 res
Backtracking + 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 res
స్టెప్-త్రూ లోడ్ అవుతోంది…

Palindrome 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.