Scribbling

[C++] isalnum, tolower 본문

Computer Science/C++

[C++] isalnum, tolower

focalpoint 2023. 8. 18. 06:54

 

LeetCode 125. Valid Palindrome

https://leetcode.com/problems/valid-palindrome/

 

Valid Palindrome - LeetCode

Can you solve this real interview question? 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 cha

leetcode.com

class Solution {
public:
    bool isPalindrome(string s) {
        vector<char> vec;
        for (char c : s) {
            if (isalnum(c)) {
                vec.push_back(tolower(c));
            }
        }
        if (vec.empty()) {
            return true;
        }
        auto left = vec.begin();
        auto right = vec.end() - 1;
        while (left < right) {
            if (*left != *right) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
};