본문 바로가기
LeetCode

125. Valid Palindrome 문제

by O_x 2022. 7. 10.

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 otherwise.

 

모든 대문자를 소문자로 변환하고 영숫자가 아닌 문자를 모두 제거한 후
앞뒤로 동일하게 읽는 경우 구는 회문입니다.
영숫자 문자에는 문자와 숫자가 포함됩니다. 
문자열 s가 주어지면 회문이면 true를 반환하고 그렇지 않으면 false를 반환합니다.


앞에서 뒤에서 시작해서 하나하나 조사하여 같은 단어인 경우ture
ex)rar, pip

Example 1:

Input: s = "A man, a plan, a canal: Panama"
Output: true
Explanation: "amanaplanacanalpanama" is a palindrome.

Example 2:

Input: s = "race a car"
Output: false
Explanation: "raceacar" is not a palindrome.

Example 3:

Input: s = " "
Output: true
Explanation: s is an empty string "" after removing non-alphanumeric characters.
Since an empty string reads the same forward and backward, it is a palindrome.

 

Constraints:

  • 1 <= s.length <= 2 * 105
  • s consists only of printable ASCII characters.

이 문제는 아스키 코드에 대한 개념을 활용하여 쉽게 풀 수 있다

대문자,소문자 의 범위를 측정해서 문자인지 아닌지를 구별해 낼 수 있다.

소문자에는 32를 더하여 대문자로 만들어 준다.

class Solution {
public:
    bool isPalindrome(string s) {
        
        if( s.empty() ) return true;
        
        vector<char> vec;                 
        /*-------------------*/
        /* ASCII             */
        /*  0-9 : 48-57      */
        /*  A-Z : 65-90      */
        /*  a-z : 97-122     */
        /*-------------------*/
        for (char c : s) 
        {                        
            if( c >= 65 && c <= 90  ){  vec.push_back(c+32);    }            
            if( c >= 97 && c <= 122 ){  vec.push_back(c);       }            
            if( c >= 48 && c <= 57  ){  vec.push_back(c);       }            
        }    
        
        if( vec.size() == 1 ) 
            return true;
       
        int L = 0;
        int R = vec.size()-1;
        for(int idx = 0; idx < vec.size()/2 ; ++idx)
        {
            if( vec[L] != vec[R] )
                return false;
            
            L++;
            R--;            
        }
        return true;
    }
};

운영중인 카톡방입니다.

https://open.kakao.com/o/gsMhUFie

 

C/C++/C# 언리얼/유니티 /질문

#C++#C#언리얼#게임개발#질문#개발#자료구조#백준#프로그래머스#c#유니티#unity#enreal

open.kakao.com

 

'LeetCode' 카테고리의 다른 글

20. Valid Parentheses 문제풀이  (0) 2022.07.13
415. Add Strings 문제 c++  (0) 2022.07.10
796. Rotate String 문제  (0) 2022.07.07
28. Implement strStr() c++  (0) 2022.07.07
1. Two Sum Leetcode 문제  (0) 2022.07.04

댓글