problem:
solution:
class Solution {
public:
// instead use isalnum() function.
bool valid(char a) {
if ((a >= 'A' && a <= 'Z') ||
(a >= 'a' && a <= 'z') ||
(a >= '0' && a <= '9'))
return true;
return false;
}
bool equal (char a, char b) {
if (tolower(a) == tolower(b))
return true;
return false;
}
bool isPalindrome(string s) {
int size = s.size();
int start = 0, end = size - 1;
while (start < end) {
while (!valid(s[start]) && start < end)
start++;
while (!valid(s[end]) && start < end)
end--;
if (!equal(s[start++], s[end--]))
return false;
}
return true;
}
};
============
Shorter one: ( isalnum() and tolower() helper function in c++ )
bool isPalindrome(string s) {
int size = s.size();
int start = 0, end = size - 1;
while (start < end) {
while (!isalnum(s[start]) && start < end)
start++;
while (!isalnum(s[end]) && start < end)
end--;
if (tolower(s[start++]) != tolower(s[end--]))
return false;
}
return true;
}
==== Third one ======
bool isPalindrome(string s) {
bool ans = false;
if (s.size() == 0) {
return true;
}
int left = 0, right = s.size() - 1;
while (left <= right) {
while(!isalnum(s[left])) {
left++;
}
while(!isalnum(s[right])) {
right--;
}
if (left >= right) {
return true;
} else if(tolower(s[left++]) != tolower(s[right--])) {
return false;
}
}
return true;
}
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.
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.
solution:
class Solution {
public:
// instead use isalnum() function.
bool valid(char a) {
if ((a >= 'A' && a <= 'Z') ||
(a >= 'a' && a <= 'z') ||
(a >= '0' && a <= '9'))
return true;
return false;
}
bool equal (char a, char b) {
if (tolower(a) == tolower(b))
return true;
return false;
}
bool isPalindrome(string s) {
int size = s.size();
int start = 0, end = size - 1;
while (start < end) {
while (!valid(s[start]) && start < end)
start++;
while (!valid(s[end]) && start < end)
end--;
if (!equal(s[start++], s[end--]))
return false;
}
return true;
}
};
============
Shorter one: ( isalnum() and tolower() helper function in c++ )
bool isPalindrome(string s) {
int size = s.size();
int start = 0, end = size - 1;
while (start < end) {
while (!isalnum(s[start]) && start < end)
start++;
while (!isalnum(s[end]) && start < end)
end--;
if (tolower(s[start++]) != tolower(s[end--]))
return false;
}
return true;
}
==== Third one ======
bool isPalindrome(string s) {
bool ans = false;
if (s.size() == 0) {
return true;
}
int left = 0, right = s.size() - 1;
while (left <= right) {
while(!isalnum(s[left])) {
left++;
}
while(!isalnum(s[right])) {
right--;
}
if (left >= right) {
return true;
} else if(tolower(s[left++]) != tolower(s[right--])) {
return false;
}
}
return true;
}
No comments:
Post a Comment