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: TrueExample 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.

    Runtime: 76 ms, faster than 94.71% of C++ online submissions for Valid Palindrome II.
    Memory Usage: 1 MB, less than 77.08% of C++ online submissions for Valid Palindrome II.

    1. class Solution {
    2. public:
    3. bool validPalindrome(string s) {
    4. for (int i = 0, j = s.size() - 1; i < j; i++, j--) {
    5. if (s[i] != s[j]) {
    6. int i1 = i;
    7. int j1 = j - 1;
    8. int i2 = i + 1;
    9. int j2 = j;
    10. while (i1 < j1 && s[i1] == s[j1]) {
    11. i1++;
    12. j1--;
    13. }
    14. while (i2 < j2 && s[i2] == s[j2]) {
    15. i2++;
    16. j2--;
    17. }
    18. return i1 >= j1 || i2 >= j2;
    19. }
    20. }
    21. return true;
    22. }
    23. };

    image.png