Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 | 31 |
Tags
- RxJS 에러 처리
- 알고리즘스터디
- leetcode
- Climbing Stairs
- contains duplicate
- Observable vs Array
- RxJS 생성 오퍼레이터
- 협업문화
- leedcode
- React hooks 남용 사례
- React useCallback 사용법
- RxJS 마블 다이어그램
- RxJS 오퍼레이터
- useCallback 성능 최적화
- 개발자커뮤니케이션
- DaleStudy
- Blind75
- 자바스크립트 고차 함수 vs Observable
- React useEffect 안티패턴
- 달래스터디
- RxJS 멀티캐스팅
- useMemo 성능 최적화
- 알고리즘
- React 리렌더링 최적화
- RxJS 변환 오퍼레이터
- 스쿼드조직
- RxJS 함수형 프로그래밍
- RxJS 결합 오퍼레이터
- React 성능 최적화 방법
- React useMemo 사용법
Archives
- Today
- Total
수쿵의 IT월드
DaleStudy | Leetcode Study 3주차 - Valid Palindrome 본문

문제
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.
Constraints:
- 1 <= s.length <= 10^5
- s consists only of printable ASCII characters.
풀이
function isPalindrome(s: string): boolean {
const cleanString = s.toLowerCase().replace(/\s+/g, '').replace(/[^a-z0-9]/g, '');
let left = 0;
let right = cleanString.length - 1;
while (left < right) {
if (cleanString[left] !== cleanString[right]) {
return false;
}
left++;
right--;
}
return true;
};
이 문제는 비교적 단순한데, replace를 이용해서 특수문자와 공백을 제거한 뒤에 왼쪽과 오른쪽을 하나씩 옮겨가며 체크하면 된다. 2개의 문자가 다르면 false를 리턴하고 루프문이 끝나면 모든 문자가 동일하다는 뜻이므로 true를 리턴해주면 된다.