본문 바로가기
Problem Solving/LeetCode

[LeetCode] 680. Valid Palindrome II - Java

by graycode 2026. 9. 22.

• 문제 링크

 

Valid Palindrome II - LeetCode

Can you solve this real interview question? Valid Palindrome II - Given a string s, return true if the s can be palindrome after deleting at most one character from it.   Example 1: Input: s = "aba" Output: true Example 2: Input: s = "abca" Output: true E

leetcode.com

 

• 풀이 코드

public class Solution {

    public boolean validPalindrome(String s) {
        int l = -1, r = s.length();
        while (++l < --r) if (s.charAt(l) != s.charAt(r)) return isPalindrome(s, l + 1, r) || isPalindrome(s, l, r - 1);

        return true;
    }

    private boolean isPalindrome(String s, int l, int r) {
        while (l < r) if (s.charAt(l++) != s.charAt(r--)) return false;

        return true;
    }

}

댓글