본문 바로가기
Programming/LeetCode

[LeetCode] Valid Palindrome II

by 데이터현 2022. 4. 2.

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

 

'Programming > LeetCode' 카테고리의 다른 글

[LeetCode] Tree Node  (0) 2022.04.03
[LeetCode] Next Permutation  (0) 2022.04.03
[LeetCode] Department Highest Salary  (0) 2022.03.31
[LeetCode] Customers Who Never Order  (0) 2022.03.31
[LeetCode] Duplicate Emails  (0) 2022.03.31

댓글