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.