Palindromic Substrings
LeetCode 647 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Count expansions from 2n-1 centres.
நான் 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
expand_both_side returns how many palindromes share this centre.
- 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.