[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 a palindrome.

Note:
Are you consider that the string might is empty? This was a good question to ask during a interview.

For the purpose of this problem, we define empty string as valid palindrome.
Title: Determine if a string is a string of words back
Idea: Two pointers from the string's head and tail to the middle, only to encounter is the word characters comparison is equal, any one of the pointers encountered other symbols continue to walk until the character or the character boundaries.
Attention:
1, empty characters are also palindrome
2, must be the boundary to judge, otherwise when the string only special symbol without the letter will be out of bounds, in VS2012 incredibly cross-border program did not crash, Linux also did not.
3. Case insensitive
4, when strlen (s) < 2 must be a palindrome

#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 '))BOOLIspalindrome (Char* s) {if(NULL = = s)return true;if(' + '= = s)return true;if(strlen(s) <2)return true;Char* PA = s;Char* PB = S;Char* L = s;//border of PA Point     while(*PB! =' + ') pb++; pb--;//make PB point the last Character,nor '!    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;}intMain () {Char* s ="A man, a plan, a Canal:panama";BOOLR = Ispalindrome (s);printf("s is ispalindrome?:%d \ n", R);Char*S1 ="";BOOLR1 = Ispalindrome (S1);printf("S1 is Ispalindrome?:%d \ n", R1);Char*S2 ="*.";BOOLr2 = ispalindrome (s2);printf("S2 is ispalindrome?:%d \ n", r2);Char*S3 ="Sore was I ere I saw Eros.";BOOLR3 = Ispalindrome (S3);printf("S3 is Ispalindrome?:%d \ n", R3);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[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.