680. Valid Palindrome II

Problem:

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:

  1. The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
  2. </ol> </p>

    Solutions:

    class Solution {
    public:
        bool validPalindrome(string s) {
            int left = 0;
            int right = s.length() - 1;
            while (left < right) {
                if (s[left] != s[right]) {
                    return isPanlindrome(s, left + 1, right) || isPanlindrome(s, left, right - 1);
                }
                ++left;
                --right;
            }
    
            return true;
        }
    
    private:
        bool isPanlindrome(string& s, int left, int right) {
            while (left < right) {
                if (s[left] != s[right])    return false;
                ++left;
                --right;
            }
    
            return true;
        }
    };
    

    results matching ""

      No results matching ""