Programming/LeetCode
[LeetCode] Valid Palindrome II
데이터현
2022. 4. 2. 19:34
https://leetcode.com/problems/valid-palindrome-ii/submissions/
Valid Palindrome II - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
투 포인터를 활용하면 되는 문제
class Solution(object):
def validPalindrome(self, s):
"""
:type s: str
:rtype: bool
"""
# Time: O(n)
# Space: O(n)
left, right = 0, len(s) - 1
while left < right:
if s[left] != s[right]:
one, two = s[left:right], s[left + 1:right + 1]
return one == one[::-1] or two == two[::-1]
left, right = left + 1, right - 1
return True
투 포인터가 떠오르지 않아 싱가포르 형의 도움을 받았다.
- https://leetcode.com/problems/valid-palindrome-ii/discuss/107718/Easy-to-Understand-Python-Solution