125. Valid Palindrome
https://leetcode.com/problems/valid-palindrome/description/
Input: "A man, a plan, a canal: Panama"
Output: trueInput: "race a car"
Output: falseThoughts
Code
/*
* @lc app=leetcode id=125 lang=cpp
*
* [125] Valid Palindrome
*/
// @lc code=start
class Solution {
public:
bool isPalindrome(string s) {
for (int i = 0, j = s.length() - 1; i < j;) {
if (!isalpha(s[i]) && !isdigit(s[i])) {
++i;
} else if (!isalpha(s[j]) && !isdigit(s[j])) {
--j;
} else {
if (tolower(s[i]) != tolower(s[j])) {
return false;
}
++i;
--j;
}
}
return true;
}
};
// @lc code=end
Analysis
Last updated