Valid Palindrome
LeetCode 125 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Two pointers that skip non-alphanumerics.
நான் Valid Palindrome (LeetCode 125) ஐ இப்படித் தீர்த்தேன் — Two Pointers பேட்டர்னின் ஒரு எளிது பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
Compare characters from both ends, skipping anything that isn't a letter or digit and ignoring case. Any mismatch means it's not a palindrome.
Approach
- 1
left at 0, right at the end.
- 2
Move each pointer past non-alphanumeric characters.
- 3
Compare lower-cased characters; mismatch → False.
- 4
Step both inward until they meet.
நான் எப்படித் தீர்த்தேன்
Two pointersPythonO(n) time · O(1) space
class Solution:
def isPalindrome(self, s: str) -> bool:
left=0
right=len(s)-1
while left<right:
while left<right and not s[left].isalnum():
left+=1
while left<right and not s[right].isalnum():
right-=1
if s[left].lower()!=s[right].lower():
return False
left+=1
right-=1
return True
Valid Palindrome — அடிக்கடி கேட்கப்படும் கேள்விகள்
Valid Palindrome பின்னாலுள்ள intuition என்ன?
Compare characters from both ends, skipping anything that isn't a letter or digit and ignoring case. Any mismatch means it's not a palindrome.
Valid Palindrome (LeetCode 125) ஐ எப்படித் தீர்ப்பது?
1. left at 0, right at the end. 2. Move each pointer past non-alphanumeric characters. 3. Compare lower-cased characters; mismatch → False. 4. Step both inward until they meet.
Valid Palindrome இன் time மற்றும் space complexity என்ன?
Time: O(n). Space: O(1). (Accepted approach: Two pointers.)
Valid Palindrome இல் பொதுவான தவறுகள் என்ன?
Keep the left < right guard inside the skip loops so they can't cross.