[LeetCode] 125. Valid Palindrome - c++

ha·2022년 1월 19일
0

프로그래머스

목록 보기
20/21

c++풀이

bool isPalindrome(string s) {
        string str("");
        for(int i=0;i<s.size();i++){
            if(isalnum(s[i])) str.push_back(tolower(s[i]));
        }
        for(int i=0;i<str.size()/2;i++){
            if(str[i]!=str[str.size()-1-i]) return false;
        }
        return true;
}

python 정규표현식

class Solution:
    def isPalindrome(self, s: str) -> bool:
        s=s.lower()
        s=re.sub('[^a-z0-9]','',s)
        
        return s==s[::-1]
    

0개의 댓글