leetcode--Valid Palindrome
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.
#include<iostream>
#include<string.h>
using namespace std;
bool isPalindrome(string s)
{
string temp;
if(s.length() == 0 || s.length() == 1)
return 1;
for(int i = 0;i<s.length();i++)
{
if(s[i]>='a' && s[i]<='z')
{
temp += s[i];
}
else if(s[i] >= 'A' && s[i] <= 'Z')
{
temp += (char)((int)s[i]+32);
}
else if(s[i]>='0' && s[i]<='9')
{
temp += s[i];
}
}
int low = 0;
int high = temp.length()-1;
bool result = true;
if(temp.length()==0 || temp.length()==1)
return 1;
while(low < high)
{
if(temp[low] != temp[high])
{
result = false;
return result;
}
else
{
low ++;
high --;
}
}
return result;
}
int main()
{
//string s = "a.";
cout << isPalindrome(s) << endl;
system("pause");
return 0;
#include<string.h>
using namespace std;
bool isPalindrome(string s)
{
string temp;
if(s.length() == 0 || s.length() == 1)
return 1;
for(int i = 0;i<s.length();i++)
{
if(s[i]>='a' && s[i]<='z')
{
temp += s[i];
}
else if(s[i] >= 'A' && s[i] <= 'Z')
{
temp += (char)((int)s[i]+32);
}
else if(s[i]>='0' && s[i]<='9')
{
temp += s[i];
}
}
int low = 0;
int high = temp.length()-1;
bool result = true;
if(temp.length()==0 || temp.length()==1)
return 1;
while(low < high)
{
if(temp[low] != temp[high])
{
result = false;
return result;
}
else
{
low ++;
high --;
}
}
return result;
}
int main()
{
//string s = "a.";
cout << isPalindrome(s) << endl;
system("pause");
return 0;
}