Skip to content

Commit 7297914

Browse files
committed
125.valid_palindrome
1 parent f21db46 commit 7297914

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
|36|[Valid Sudoku](./leetcode/0036.valid_sudoku/)|[Go](./leetcode/0036.valid_sudoku/36.valid_sudoku.go)|Medium|Completed|
1010
|49|[Group Anagrams](./leetcode/0049.group_anagrams/)|[Go](./leetcode/0049.group_anagrams/49.group_anagrams.go)|Medium|Completed|
1111
|121|[Best Time to Buy and Sell Stock](./leetcode/0121.best_time_to_buy_and_sell_stock/)|[Go](./leetcode/0121.best_time_to_buy_and_sell_stock/121.best_time_to_buy_and_sell_stock.go)|Easy|Completed|
12+
|125|[Valid Palindrome](./leetcode/0125.valid_palindrome/)|[Go](./leetcode/0125.valid_palindrome/125.valid_palindrome.go)|Easy|Completed|
1213
|128|[Longest Consecutive Sequence](./leetcode/0128.longest_consecutive_sequence/)|[Go](./leetcode/0128.longest_consecutive_sequence/128.longest_consecutive_sequence.go)|Medium|Completed|
1314
|217|[Contains Duplicate](./leetcode/0217.contains_duplicate/)|[Go](./leetcode/0217.contains_duplicate/217.contains_duplicate.go)|Easy|Completed|
1415
|238|[Product of Array Except Self](./leetcode/0238.product_of_array_except_self/)|[Go](./leetcode/0238.product_of_array_except_self/238.product_of_array_except_self.go)|Medium|Completed|
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package data_structures_algorithms
2+
3+
import (
4+
"strings"
5+
"unicode"
6+
)
7+
8+
func isPalindrome(s string) bool {
9+
mapFunc := func(r rune) rune {
10+
if !unicode.IsLetter(r) && !unicode.IsDigit(r) {
11+
return -1
12+
}
13+
14+
return unicode.ToLower(r)
15+
}
16+
17+
s = strings.Map(mapFunc, s)
18+
19+
i, j := 0, len(s)-1
20+
for i < j {
21+
if s[i] != s[j] {
22+
return false
23+
}
24+
i++
25+
j--
26+
}
27+
return true
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [125. Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)
2+
A phrase is a palindrome if, after converting all uppercase letters to lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
3+
4+
Given a string `s`, return `true` if it is a palindrome, or `false` otherwise.
5+
6+
#### Example 1:
7+
```shell
8+
Input: s = "A man, a plan, a canal: Panama"
9+
Output: true
10+
Explanation: "amanaplanacanalpanama" is a palindrome.
11+
```
12+
13+
#### Example 2:
14+
```shell
15+
Input: s = "race a car"
16+
Output: false
17+
Explanation: "raceacar" is not a palindrome.
18+
```
19+
20+
#### Example 3:
21+
```shell
22+
Input: s = " "
23+
Output: true
24+
Explanation: s is an empty string "" after removing non-alphanumeric characters.
25+
Since an empty string reads the same forward and backward, it is a palindrome.
26+
```
27+
28+
<br>
29+
30+
#### Constraints:
31+
- <code>1 <= s.length <= 2 * 10<sup>5</sup></code>
32+
- `s` consists only of printable ASCII characters.

0 commit comments

Comments
 (0)