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.