leetcode 680. Valid Palindrome II Problem Solution [C++/Java/Python]

Keywords: C++ Java Python

680. Verification of palindrome strings II
680. Valid Palindrome II

Title:

Given a non-empty string s, delete at most one character. Determine whether it can be a palindrome string.

Example 1:

Input: "aba"
Output: True

Example 2:

Input: "abca"
Output: True
Interpretation: You can delete the c character.

Be careful:

The string contains only lowercase letters from a-z. The maximum length of a string is 50000.

Explanation:

Create two indexes, left and right, left from left to right, right from right to left until the characters in the two positions are different. If the two indexes can meet, it means that the original string is palindrome string. If it stops halfway, then it can decide whether the remaining substrings between [left:right] can delete the endpoint left or right and form palindrome string. If one of them can form a palindrome string, return True or False.

Skill:

Create two indexes, left and right.

C++
class Solution {
public:
    bool validPalindrome(string s) {
        int n = s.size();
        int i = 0;//Index from left to right
        int j = n - 1;//Index from right to left
        while(i <= j  && s[i] == s[j]){
            i++;
            j--;
        }
        if(i > j) {//An index encounter indicates that the original string is a palindrome string
            return true;
        }
        else{// Detecting whether the substrings between [left, right] can form palindrome strings by removing left or right
            string s1 = s.substr(i, j - i);
            string s2 = s.substr(i+1, j - i);
            return palindrome(s1) || palindrome(s2);
        }

    }

    bool palindrome(string s){//The Judgment of Palindrome Strings
        int i = 0;
        int j = s.size() - 1;
        while(i <= j && s[i] == s[j]){
            i++;
            j--;
        }
        return i > j;
    }
};
Java
class Solution {
    public boolean validPalindrome(String s) {
        int n = s.length();
        int left = 0, right = n - 1;
        while(left < right && s.charAt(left) == s.charAt(right)) {//Find the first different character
            left++;
            right--;
        }
        if(left >= right) {
            return true;
        }
        //Get substrings like a****b
        String sub1 = s.substring(left, right);//Check if a **** is a palindrome string?
        String sub2 = s.substring(left+1, right+1);//Check whether **** b is a palindrome string?
        return palindrome(sub1) || palindrome(sub2);
    }

    private boolean palindrome(String s) {
        int n = s.length();
        int left = 0, right = n - 1;
        while(left < right && s.charAt(left) == s.charAt(right)) {
            left++;
            right--;
        }
        return left >= right ? true : false;
    }
}
Python
class Solution(object):
    def validPalindrome(self, s):
        """
        :type s: str
        :rtype: bool
        """
        n = len(s)
        i, j = 0, n - 1
        while (i <= j and s[i] == s[j]): # Detecting the first different character from left to right
            i += 1
            j -= 1
        if i > j: # The original string is palindrome string
            return True
        # Get substrings like a****b
        return self.palindrome(s[i:j]) or self.palindrome(s[i+1:j+1]) # Check if a **** or **** b is a palindrome string?

    def palindrome(self, s): # Check palindrome strings
        n = len(s)
        i, j = 0, n - 1
        while(i <= j and s[i] == s[j]):
            i += 1
            j -= 1
        if i > j:
            return True
        else:
            return False
Reference material

https://leetcode.com/problems/valid-palindrome-ii/discuss/107720/Easy-and-concise-solution-C++JavaPython

Posted by objNoob on Sun, 03 Feb 2019 21:30:16 -0800