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.