Leetcode-125. Valid palindrome && 680. Valid palindrome Ii__leetcode

Source: Internet
Author: User
Tags lowercase alphanumeric characters

This is the question of whether the string is a palindrome, first look at the first way:

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring.

For example,
"A mans, a plan, a Canal:panama" is a palindrome.
" Race a car ' is not a palindrome.

Note:
Have your consider that the string might is empty? This is a good question to ask during a interview.

For the purpose of this problem, we define empty string as valid palindrome.

The subject is for a string (which may contain various symbols) to determine whether the letters and numbers can constitute a palindrome string, regardless of the difference between the case. The topic is simple, the use of Java built-in functions to determine whether a character is a letter or a number, if not then the next character can be judged:

    45% public
    Static Boolean ispalindrome (String s) {
        s = s.tolowercase ();
        char [] res = S.tochararray ();
        int i=0, j=res.length-1;
        while (I <= j) {
            //not letters or numbers traverse the next while
            (I<=j &&!) Character.isletterordigit (Res[i]) i++;
            while (I<=j &&!) Character.isletterordigit (Res[j]) j--;
            if (Res[i]!= res[j]) return
                false;
            else{
                i++;
                j--
            }
        }
        return true;
    }

In addition, we can use regular expressions to solve, but less efficient can only beat 8% of users:

    8.8% Public
    Boolean isPalindrome1 (string s) {
        string actual = S.replaceall ("[^a-za-z0-9]", ""). toLowerCase ();
        String rev = new StringBuffer (actual). Reverse (). toString ();
        Return Actual.equals (rev);
    

The

Then also sees a very efficient code in the discuss, the idea is the same, but here the author uses an array to determine whether a character is a letter or a number, which is highly efficient and can beat 99. 5% of the users, the code looks like this:

    99.5%
    private static final char[]charmap = new char[256];
    static{for
        (int i=0;i<10;i++) {
            charmap[i+ ' 0 '] = (char) (1+i);  Numeric
        } for
        (int i=0;i<26;i++) {
            charmap[i+ ' a '] = charmap[i+ ' a '] = (char) (11+i);  Alphabetic, ignore cases
        }
    } public

    Boolean isPalindrome2 (String s) {
        char[]pchars = S.tochararray ();
        int start = 0,end=pchars.length-1;
        char Cs,ce;
        while (start<end) {
            CS = Charmap[pchars[start]];
            CE = Charmap[pchars[end]];
            Returns 0 if
            (cs!=0 && ce!=0) {if
                (CS!=CE) return False if it is not a letter or number;
                start++;
                end--;
            } else{
                if (cs==0) start++;
                if (ce==0) end--
            }
        }
        return true;
    }

Next, look at the second question:

Given a non-empty string s, you could delete at most one character. Judge whether 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 would only
contain lowercase characters a-Z. The maximum length of the string is 50000.

The subject is to see the qualification only lowercase letters in the string to remove any one character can be a palindrome string, the method is also relatively simple, is to judge one after another, when the appearance does not want to wait until the time to try to delete a two, and then determine whether the remaining strings can be formed palindrome string. The code looks like this:

    public Boolean validPalindrome1 (String s) {
        char[] res = S.tochararray ();
        int L =-1, R = s.length ();
        while (++l <--r)
            if (Res[l]!= res[r]) return ispalindromic (res, L, r+1) | | ispalindromic (RES, L-1, R);
        return true;
    }

    public boolean ispalindromic (char[] res, int l, int r) {while
        (++l <--r)
            if (res[l)!= Res[r]) return False ;
        return true;
    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.