Home LeetCode - 125. Valid Palindrome
Post
Cancel

LeetCode - 125. Valid Palindrome

125. Valid Palindrome - easy

문제

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Note: For the purpose of this problem, we define empty string as valid palindrome.

제한사항

  • s consists only of printable ASCII characters.

입출력 예

1
2
3
4
Example 1:

Input: "A man, a plan, a canal: Panama"
Output: true
1
2
3
4
Example 2:

Input: "race a car"
Output: false

풀이

  • String
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
    bool isPalindrome(string s) {
        string onlyS;
        
        for(auto &i : s){
            if(('A' <= i && i <= 'Z') || ('0' <= i && i <= '9'))
                onlyS.push_back(i);
            else if('a' <= i && i <= 'z')
                onlyS.push_back(std::toupper(i));
        }
            
        if(onlyS.size() < 2)
            return true;
        
        for(auto i = 0 ; i < onlyS.size() ; ++i)
            if(onlyS[i] != onlyS[onlyS.size() -1 - i])
                return false;
        
        return true;
    }
};
This post is licensed under CC BY 4.0 by the author.