Tensorum
YouTubeనేర్చుకోవడం ప్రారంభించండి
LeetCode 125 · Two Pointers

Valid Palindrome

LeetCode 125 · సులభం · పరిష్కారం, intuition & దశలవారీ వాక్‌త్రూ

Two pointers that skip non-alphanumerics.

సులభంTwo PointersString
LeetCode లో తెరవండి ↗

నేను 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. 1

    left at 0, right at the end.

  2. 2

    Move each pointer past non-alphanumeric characters.

  3. 3

    Compare lower-cased characters; mismatch → False.

  4. 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.