Leetcode: Valid palindrome

Source: Internet
Author: User
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome.Note:Have you consider that the string might be empty? This is a good question to ask during an interview.For the purpose of this problem, we define empty string as valid palindrome.

Difficulty: 79. This question is used to determine whether a string is a return string. Because the algorithm is relatively simple to look at a string, it starts from two ends and goes to the middle to perform matching. In this case, only letters and numbers are judged in the requirement. You can skip other characters directly. Therefore, we need to write a function to determine whether the character is valid or not. Because case sensitivity is ignored, if the two characters are the same, convert them to uppercase and lowercase letters. This algorithm only needs a linear scan from scanning on both sides to meeting in the middle. The complexity is O (n), and the space is O (1 ).

 1 public class Solution { 2     public boolean isPalindrome(String s) { 3         if (s == null || s.length() == 0) { 4             return true; 5         } 6         int l = 0; 7         int r = s.length() - 1; 8         while (l < r) { 9             while (l<s.length() && !isValidChar(s.charAt(l))) {10                 l++;11             }12             while (r>0 && !isValidChar(s.charAt(r))) {13                 r--;14             }15             if (l < r) {16                 if (isSame(s.charAt(l), s.charAt(r))) {17                     l++;18                     r--;19                 }20                 else return false;21             }22         }23         return true;24     }25     26     public boolean isValidChar(char c) {27         if ((c>=‘a‘ && c<=‘z‘) || (c>=‘A‘ && c<=‘Z‘) || (c>=‘0‘ && c<=‘9‘))28             return true;29         else return false;30     }31     32     public boolean isSame(char s1, char s2) {33         if (s1>=‘A‘ && s1<=‘Z‘) {34             s1 = (char)(s1 - ‘A‘ + ‘a‘);35         }36         if (s2>=‘A‘ && s2<=‘Z‘) {37             s2 = (char)(s2 - ‘A‘ + ‘a‘);38         }39         if (s1 == s2) {40             return true;41         }42         else return false;43     }44 }

 

Leetcode: Valid palindrome

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.