Skip to content

Commit dbd4dda

Browse files
authored
Merge pull request #1021 from 0xff-dev/2490
Add solution and test-cases for problem 2490
2 parents 8a19dff + 92ebe8d commit dbd4dda

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

leetcode/2401-2500/2490.Circular-Sentence/README.md

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,53 @@
11
# [2490.Circular Sentence][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+
A **sentence** is a list of words that are separated by a **single** space with no leading or trailing spaces.
5+
6+
- For example, `"Hello World"`, `"HELLO"`, `"hello world hello world"` are all sentences.
7+
8+
Words consist of **only** uppercase and lowercase English letters. Uppercase and lowercase English letters are considered different.
9+
10+
A sentence is **circular** if:
11+
12+
- The last character of a word is equal to the first character of the next word.
13+
- The last character of the last word is equal to the first character of the first word.
14+
15+
For example, `"leetcode exercises sound delightful"`, `"eetcode"`, `"leetcode eats soul"` are all circular sentences. However, `"Leetcode is cool"`, `"happy Leetcode"`, `"Leetcode"` and `"I like Leetcode"` are **not** circular sentences.
16+
17+
Given a string `sentence`, return `true` if it is circular. Otherwise, return `false`.
718

819
**Example 1:**
920

1021
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
22+
Input: sentence = "leetcode exercises sound delightful"
23+
Output: true
24+
Explanation: The words in sentence are ["leetcode", "exercises", "sound", "delightful"].
25+
- leetcode's last character is equal to exercises's first character.
26+
- exercises's last character is equal to sound's first character.
27+
- sound's last character is equal to delightful's first character.
28+
- delightful's last character is equal to leetcode's first character.
29+
The sentence is circular.
1330
```
1431

15-
## 题意
16-
> ...
17-
18-
## 题解
32+
**Example 2:**
1933

20-
### 思路1
21-
> ...
22-
Circular Sentence
23-
```go
34+
```
35+
Input: sentence = "eetcode"
36+
Output: true
37+
Explanation: The words in sentence are ["eetcode"].
38+
- eetcode's last character is equal to eetcode's first character.
39+
The sentence is circular.
2440
```
2541

42+
**Example 3:**
43+
44+
```
45+
Input: sentence = "Leetcode is cool"
46+
Output: false
47+
Explanation: The words in sentence are ["Leetcode", "is", "cool"].
48+
- Leetcode's last character is not equal to is's first character.
49+
The sentence is not circular.
50+
```
2651

2752
## 结语
2853

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(sentence string) bool {
4+
l := len(sentence)
5+
if sentence[0] != sentence[l-1] {
6+
return false
7+
}
8+
for i := range sentence {
9+
if sentence[i] == ' ' {
10+
if sentence[i-1] != sentence[i+1] {
11+
return false
12+
}
13+
}
14+
}
15+
return true
516
}

leetcode/2401-2500/2490.Circular-Sentence/Solution_test.go

Lines changed: 6 additions & 6 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
13+
inputs string
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "leetcode exercises sound delightful", true},
17+
{"TestCase2", "eetcode", true},
18+
{"TestCase3", "Leetcode is cool", false},
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)