Valid Palindrome II
LeetCode 680 · எளிது · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
On the first mismatch, try both deletions once.
நான் Valid Palindrome II (LeetCode 680) ஐ இப்படித் தீர்த்தேன் — Two Pointers பேட்டர்னின் ஒரு எளிது பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
Walk inward from both ends. At the first mismatch you get one deletion: try skipping the left character or the right character, and check whether what remains between the pointers is a palindrome.
Approach
- 1
Two pointers from the ends.
- 2
Matching characters: move inward.
- 3
Mismatch: return palindrome(i+1, j) or palindrome(i, j−1).
நான் எப்படித் தீர்த்தேன்
Two pointers + one skipPythonO(n) time · O(1) space
class Solution:
def check_palindrome(self,i,j,s) ->bool:
while i<j:
if s[i]!=s[j]:
return False
i+=1
j-=1
return True
def validPalindrome(self, s: str) -> bool:
i,j=0,len(s)-1
while(i<j):
#Found a mismatched pair, try both deletions
if s[i]!=s[j]:
return self.check_palindrome(i+1,j,s) or self.check_palindrome(i,j-1,s)
i=i+1
j=j-1
return TrueValid Palindrome II — அடிக்கடி கேட்கப்படும் கேள்விகள்
Valid Palindrome II பின்னாலுள்ள intuition என்ன?
Walk inward from both ends. At the first mismatch you get one deletion: try skipping the left character or the right character, and check whether what remains between the pointers is a palindrome.
Valid Palindrome II (LeetCode 680) ஐ எப்படித் தீர்ப்பது?
1. Two pointers from the ends. 2. Matching characters: move inward. 3. Mismatch: return palindrome(i+1, j) or palindrome(i, j−1).
Valid Palindrome II இன் time மற்றும் space complexity என்ன?
Time: O(n). Space: O(1). (Accepted approach: Two pointers + one skip.)
Valid Palindrome II இல் பொதுவான தவறுகள் என்ன?
Only one deletion is allowed — don't recurse with another deletion budget.