일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
- Linux
- C++
- html
- postgresql
- Express
- CV
- Git
- backend
- python
- ts
- vscode
- API
- review
- ML
- PyTorch
- CSS
- nodejs
- GAN
- PRISMA
- ps
- CS
- UI
- frontend
- Three
- js
- figma
- DB
- react
- mongo
- SOLID
- Today
- Total
아카이브
[PS] Leet | 125. 팰린드롬 찾기 본문
문제
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('')
};
'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 |