[PS] Leet | 125. 팰린드롬 찾기

2025. 6. 20. 15:07·CS/PS

문제

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.

예시

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.

조건

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

답

일반적인 팰린드롬 문제이지만, 입력값에 숫자나 문자가 아닌 다른 값이 섞여 있는게 차이입니다.

 

정규식을 통해 간단하게 제거할 수 있습니다.

 

이후는 팰린드롬 문제과 동일하게 해결합니다.

/**
 * @param {string} s
 * @return {boolean}
 */
var isPalindrome = function(s) {
    const str = s.toLowerCase().replace(/[^a-z0-9]/g, '');
    return str === str.split('').reverse().join('')
};

 

728x90

'CS > PS' 카테고리의 다른 글

[PS] Leet | 105. Preorder와 Inorder 수열로부터 트리 구성하기  (0) 2025.07.09
[PS] Leet | 5. 가장 긴 팰린드롬 하위문자열  (0) 2025.06.27
[PS] Leet | 76. 가장 작은 하위 문자열의 창(window)  (0) 2025.06.19
[PS] Leet | 424. 가장 긴 반복되는 철자로 대체하기  (0) 2025.06.18
[PS] Leet | 48. 이미지 회전  (0) 2025.05.21
'CS/PS' 카테고리의 다른 글
  • [PS] Leet | 105. Preorder와 Inorder 수열로부터 트리 구성하기
  • [PS] Leet | 5. 가장 긴 팰린드롬 하위문자열
  • [PS] Leet | 76. 가장 작은 하위 문자열의 창(window)
  • [PS] Leet | 424. 가장 긴 반복되는 철자로 대체하기
Rayi
Rayi
  • Rayi
    아카이브
    Rayi
  • 전체
    오늘
    어제
    • 분류 전체보기 (262)
      • CS (40)
        • ML (3)
        • CV (2)
        • PS (34)
      • Reveiw (17)
        • Paper (17)
        • Github (0)
      • Pytorch (5)
      • Language (58)
        • Python (7)
        • JavaScript (32)
        • TypeScript (16)
        • C++ (3)
      • IDE (12)
      • Git (13)
      • Frontend (71)
        • React (8)
        • SolidJS (20)
        • CSS (12)
      • UI (3)
      • Backend (15)
        • DB (17)
        • Node.js (11)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    deploy
    CV
    ps
    CSS
    html
    js
    nodejs
    figma
    postgresql
    SOLID
    frontend
    UI
    Express
    python
    PRISMA
    mongo
    react
    Three
    Git
    DB
    backend
    ML
    API
    C++
    review
    ts
    CS
    vscode
    PyTorch
    GAN
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
Rayi
[PS] Leet | 125. 팰린드롬 찾기
상단으로

티스토리툴바