Tensorum
YouTubeसीखना शुरू करें
LeetCode 647 · Palindrome / Interval DP

Palindromic Substrings

LeetCode 647 · मध्यम · समाधान, intuition और कदम-दर-कदम वॉकथ्रू

Count expansions from 2n-1 centres.

मध्यमTwo PointersStringDynamic Programming

मैंने Palindromic Substrings (LeetCode 647) ऐसे हल किया — Palindrome / Interval DP पैटर्न का एक मध्यम प्रॉब्लम। नीचे: intuition, कदम-दर-कदम approach, मेरा accepted Python कोड, animated test cases और line-by-line debugger।

नीचे का विवरण अंग्रेज़ी में है।

Intuition

Every palindromic substring has a centre. Expanding from each centre, every successful step outward is one more palindrome — so count steps instead of tracking strings.

Approach

  1. 1

    expand_both_side returns how many palindromes share this centre.

  2. 2

    Sum over odd centres (i, i) and even centres (i, i+1).

मैंने कैसे हल किया

Expand around centrePythonO(n²) time · O(1) space
class Solution:
    def countSubstrings(self, s: str) -> int:
        #expand both side
        def expand_both_side(s,start,end):
            count = 0
            while start>=0 and end<len(s):
                if s[start]!=s[end]:
                    break
                count+=1
                start-=1
                end+=1
            return count
        count=0
        for i in range(len(s)):
            count+=expand_both_side(s,i,i)
            count+=expand_both_side(s,i,i+1)
        return count
स्टेप-थ्रू लोड हो रहा है…

Palindromic Substrings — अक्सर पूछे जाने वाले प्रश्न

Palindromic Substrings के पीछे intuition क्या है?

Every palindromic substring has a centre. Expanding from each centre, every successful step outward is one more palindrome — so count steps instead of tracking strings.

Palindromic Substrings (LeetCode 647) कैसे हल करें?

1. expand_both_side returns how many palindromes share this centre. 2. Sum over odd centres (i, i) and even centres (i, i+1).

Palindromic Substrings की time और space complexity क्या है?

Time: O(n²)। Space: O(1)। (Accepted approach: Expand around centre।)

Palindromic Substrings में आम गलतियाँ क्या हैं?

Count every expansion, not just the longest one.