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.