Skip to content

Instantly share code, notes, and snippets.

@zhoutuo
Last active December 12, 2015 09:29
Show Gist options
  • Save zhoutuo/4751595 to your computer and use it in GitHub Desktop.
Save zhoutuo/4751595 to your computer and use it in GitHub Desktop.
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
class Solution {
public:
bool isPalindrome(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string tmp = "";
for(int i = 0; i < s.length(); ++i) {
if(!islower(s[i])) {
s[i] = tolower(s[i]);
}
if(isalnum(s[i])) {
tmp += s[i];
}
}
for(int i = 0; i < tmp.length(); ++i) {
char cur = tmp[i];
char opposite = tmp[tmp.length() - i - 1];
if(cur != opposite) {
return false;
}
}
return true;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment