Last active
December 12, 2015 09:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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