From 92ebe8d15b20fb2946eef2c0e18464ebe54fdb2f Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 2 Nov 2024 09:38:37 +0800 Subject: [PATCH] Add solution and test-cases for problem 2490 --- .../2490.Circular-Sentence/README.md | 51 ++++++++++++++----- .../2490.Circular-Sentence/Solution.go | 15 +++++- .../2490.Circular-Sentence/Solution_test.go | 12 ++--- 3 files changed, 57 insertions(+), 21 deletions(-) diff --git a/leetcode/2401-2500/2490.Circular-Sentence/README.md b/leetcode/2401-2500/2490.Circular-Sentence/README.md index b03d52c8d..68f11317f 100755 --- a/leetcode/2401-2500/2490.Circular-Sentence/README.md +++ b/leetcode/2401-2500/2490.Circular-Sentence/README.md @@ -1,28 +1,53 @@ # [2490.Circular Sentence][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +A **sentence** is a list of words that are separated by a **single** space with no leading or trailing spaces. + +- For example, `"Hello World"`, `"HELLO"`, `"hello world hello world"` are all sentences. + +Words consist of **only** uppercase and lowercase English letters. Uppercase and lowercase English letters are considered different. + +A sentence is **circular** if: + +- The last character of a word is equal to the first character of the next word. +- The last character of the last word is equal to the first character of the first word. + +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. + +Given a string `sentence`, return `true` if it is circular. Otherwise, return `false`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: sentence = "leetcode exercises sound delightful" +Output: true +Explanation: The words in sentence are ["leetcode", "exercises", "sound", "delightful"]. +- leetcode's last character is equal to exercises's first character. +- exercises's last character is equal to sound's first character. +- sound's last character is equal to delightful's first character. +- delightful's last character is equal to leetcode's first character. +The sentence is circular. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Circular Sentence -```go +``` +Input: sentence = "eetcode" +Output: true +Explanation: The words in sentence are ["eetcode"]. +- eetcode's last character is equal to eetcode's first character. +The sentence is circular. ``` +**Example 3:** + +``` +Input: sentence = "Leetcode is cool" +Output: false +Explanation: The words in sentence are ["Leetcode", "is", "cool"]. +- Leetcode's last character is not equal to is's first character. +The sentence is not circular. +``` ## 结语 diff --git a/leetcode/2401-2500/2490.Circular-Sentence/Solution.go b/leetcode/2401-2500/2490.Circular-Sentence/Solution.go index d115ccf5e..91057f2c4 100644 --- a/leetcode/2401-2500/2490.Circular-Sentence/Solution.go +++ b/leetcode/2401-2500/2490.Circular-Sentence/Solution.go @@ -1,5 +1,16 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(sentence string) bool { + l := len(sentence) + if sentence[0] != sentence[l-1] { + return false + } + for i := range sentence { + if sentence[i] == ' ' { + if sentence[i-1] != sentence[i+1] { + return false + } + } + } + return true } diff --git a/leetcode/2401-2500/2490.Circular-Sentence/Solution_test.go b/leetcode/2401-2500/2490.Circular-Sentence/Solution_test.go index 14ff50eb4..68f7b1bc3 100644 --- a/leetcode/2401-2500/2490.Circular-Sentence/Solution_test.go +++ b/leetcode/2401-2500/2490.Circular-Sentence/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs string expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "leetcode exercises sound delightful", true}, + {"TestCase2", "eetcode", true}, + {"TestCase3", "Leetcode is cool", false}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }