[Leetcode]-Valid Palindrome, leetcode-valid

Source: Internet
Author: User
Tags alphanumeric characters

[Leetcode]-Valid Palindrome, leetcode-valid

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.
Question: Determine whether the string is a return string
Train of Thought: The two pointers go from the header and tail of the string to the middle. Only when the character is met Will the comparison be equal. When any pointer encounters other symbols, it will continue to go, until it is a character or has reached the character boundary.
Note:
1. Empty characters are also input characters
2. Be sure to make boundary judgments. Otherwise, when there are only special characters and no letters in the string, it will cross the border. In VS2012, the program does not crash, nor does it exist in LINUX.
3. Case Insensitive
4. When strlen (s) <2 must be a reply

#include <stdlib.h>#include <stdio.h>#include <math.h>#include <string.h>#include <stdbool.h>#define isstr(a)  ((a>='a'&&a<='z')||(a>='A'&&a<='Z')||(a>='0'&&a<='9'))bool isPalindrome(char* s) {    if(NULL == s)       return true;    if('\0' == s)       return true;    if(strlen(s) < 2)   return true;    char* pa = s;    char* pb = s;    char* l  = s;//border of pa point    while(*pb != '\0') pb++;    pb--; //make pb point the last character,nor '\0'!!    char* n = pb;//border of pb point    while(pa < pb)    {        while(!isstr(*pa) && pa<=n)  pa++;        while(!isstr(*pb) && pb>=l) pb--;        if(((*pa != *pb) && (abs(*pa-*pb) != 'a'-'A')) && (isstr(*pa)) && (isstr(*pb)))  return false;        else        {            pa++;            pb--;        }    }    return true;}int main(){    char* s = "A man, a plan, a canal: panama";    bool r = isPalindrome(s);    printf("s is isPalindrome? : %d \n",r);    char *s1 = "";    bool r1 = isPalindrome(s1);    printf("s1 is isPalindrome? : %d \n",r1);    char *s2 = "*.";    bool r2 = isPalindrome(s2);    printf("s2 is isPalindrome? : %d \n",r2);    char *s3 = "Sore was I ere I saw Eros.";    bool r3 = isPalindrome(s3);    printf("s3 is isPalindrome? : %d \n",r3);}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.