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.