Skip to content

Commit d9258db

Browse files
committed
Add solution and test-cases for problem 2108
1 parent d9d2a4a commit d9258db

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

leetcode/2101-2200/2108.Find-First-Palindromic-String-in-the-Array/README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
# [2108.Find First Palindromic String in the Array][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given an array of strings `words`, return the first **palindromic** string in the array. If there is no such string, return an **empty string** `""`.
5+
6+
A string is **palindromic** if it reads the same forward and backward.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: words = ["abc","car","ada","racecar","cool"]
12+
Output: "ada"
13+
Explanation: The first string that is palindromic is "ada".
14+
Note that "racecar" is also palindromic, but it is not the first.
1315
```
1416

15-
## 题意
16-
> ...
17+
**Example 2:**
1718

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Find First Palindromic String in the Array
23-
```go
19+
```
20+
Input: words = ["notapalindrome","racecar"]
21+
Output: "racecar"
22+
Explanation: The first and only string that is palindromic is "racecar".
2423
```
2524

25+
**Example 3:**
26+
27+
```
28+
Input: words = ["def","ghi"]
29+
Output: ""
30+
Explanation: There are no palindromic strings, so the empty string is returned.
31+
```
2632

2733
## 结语
2834

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(words []string) string {
4+
for _, str := range words {
5+
s, e := 0, len(str)-1
6+
for ; s < e && str[s] == str[e]; s, e = s+1, e-1 {
7+
}
8+
if s >= e {
9+
return str
10+
}
11+
}
12+
return ""
513
}

leetcode/2101-2200/2108.Find-First-Palindromic-String-in-the-Array/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []string
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []string{"abc", "car", "ada", "racecar", "cool"}, "ada"},
17+
{"TestCase2", []string{"notapalindrome", "racecar"}, "racecar"},
18+
{"TestCase3", []string{"def", "ghi"}, ""},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)