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.