Skip to content

Commit c335cf1

Browse files
committed
[Easy] Title: 125. Valid Palindrome - LeetCode
1 parent 1b06d40 commit c335cf1

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [Easy] 26. Remove Duplicates from Sorted Array
2+
3+
[문제 링크](https://leetcode.com/problems/valid-palindrome/)
4+
5+
### 문제 설명
6+
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.
7+
8+
Given a string s, return true if it is a palindrome, or false otherwise.
9+
10+
### 입출력
11+
Example 1:
12+
```
13+
Input: s = "A man, a plan, a canal: Panama"
14+
Output: true
15+
```
16+
Explanation: "amanaplanacanalpanama" is a palindrome.
17+
18+
Example 2:
19+
```
20+
Input: s = "race a car"
21+
Output: false
22+
```
23+
Explanation: "raceacar" is not a palindrome.
24+
25+
Example 3:
26+
```
27+
Input: s = " "
28+
Output: true
29+
```
30+
Explanation: s is an empty string "" after removing non-alphanumeric characters.
31+
Since an empty string reads the same forward and backward, it is a palindrome.
32+
33+
### 문제설명
34+
Constraints:
35+
- 1 <= s.length <= 2 * 105
36+
- s consists only of printable ASCII characters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public boolean isPalindrome(String s) {
3+
if(s == null || s.trim().isEmpty()) return true;
4+
5+
s = s.trim().toLowerCase().replaceAll("[^a-zA-Z0-9]+", "");
6+
int left = 0; int right = s.length() - 1;
7+
while(left < right) {
8+
if(s.charAt(left) != s.charAt(right)) return false;
9+
right--;
10+
left++;
11+
}
12+
13+
return true;
14+
}
15+
}

0 commit comments

Comments
 (0)