[LeetCode] Valid Palindrome

125. Valid Palindrome

A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.

Given a string s, return true if it is a palindrome, or false

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
bool isPalindrome(string s) {
string res = "";
for(auto ch : s) {
if(isalpha(ch)) res.push_back(::tolower(ch));
else if(isdigit(ch)) res.push_back(ch);
}
for(int l = 0, r = res.size() - 1; l < r; l += 1, r -= 1) {
if(res[l] != res[r]) return false;
}
return true;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/07/30/PS/LeetCode/valid-palindrome/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.