680. Valid Palindrome II
Easy
Given a non-empty string s
, you may delete at most one character. Judge whether you can make it a palindrome.
Example 1:
Input: "aba" Output: True
Example 2:
Input: "abca" Output: True Explanation: You could delete the character 'c'.
Note:
- The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Solution { public boolean validPalindrome(String s) { int n = s.length(), l = 0, r = n-1, d = 0; while(l<r){ if(s.charAt(l)!=s.charAt(r)){ return helper(s, l+1, r)||helper(s, l, r-1); } l++; r--; } return true; } boolean helper(String s, int l, int r){ while(l<r){ if(s.charAt(l)!=s.charAt(r)) return false; l++; r--; } return true; } } |
No comments:
Post a Comment