Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 1813 #1001

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions leetcode/1801-1900/1813.Sentence-Similarity-III/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,52 @@
# [1813.Sentence Similarity III][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
You are given two strings `sentence1` and `sentence2`, each representing a **sentence** composed of **words**. A sentence is a list of words that are separated by a **single** space with no leading or trailing spaces. Each word consists of only uppercase and lowercase English characters.

Two sentences `s1` and `s2` are considered **similar** if it is possible to insert an arbitrary sentence (possibly empty) inside one of these sentences such that the two sentences become equal. **Note** that the inserted sentence must be separated from existing words by spaces.

For example,

- `s1 = "Hello Jane"` and `s2 = "Hello my name is Jane"` can be made equal by inserting `"my name is"` between `"Hello"` and `"Jane"` in s1.
- `s1 = "Frog cool"` and `s2 = "Frogs are cool"` are **not** similar, since although there is a sentence `"s are"` inserted into `s1`, it is not separated from `"Frog"` by a space.

Given two sentences `sentence1` and `sentence2`, return **true** if `sentence1` and `sentence2` are **similar**. Otherwise, return **false**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: sentence1 = "My name is Haley", sentence2 = "My Haley"

Output: true

Explanation:

sentence2 can be turned to sentence1 by inserting "name is" between "My" and "Haley".
```

## 题意
> ...
**Example 2:**

## 题解
```
Input: sentence1 = "of", sentence2 = "A lot of words"

Output: false

Explanation:

No single sentence can be inserted inside one of the sentences to make it equal to the other.
```

**Example 3:**

### 思路1
> ...
Sentence Similarity III
```go
```
Input: sentence1 = "Eating right now", sentence2 = "Eating"

Output: true

Explanation:

sentence2 can be turned to sentence1 by inserting "right now" at the end of the sentence.
```

## 结语

Expand Down
40 changes: 38 additions & 2 deletions leetcode/1801-1900/1813.Sentence-Similarity-III/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
package Solution

func Solution(x bool) bool {
return x
import "strings"

// Eating, Eating right now
func match1813(s, t []string) bool {
l1, r1 := 0, len(s)-1
l2, r2 := 0, len(t)-1
for ; l1 <= r1 && s[l1] == t[l2]; l1, l2 = l1+1, l2+1 {

}
// 前缀
if l1 > r1 {
return true
}

for ; r1 >= l1 && s[r1] == t[r2]; r1, r2 = r1-1, r2-1 {
}
if r1 < l1 {
return true
}
return false
}

func Solution(sentence1 string, sentence2 string) bool {
// 相等只需要插入空的数据即可
if sentence1 == sentence2 {
return true
}
l1, l2 := len(sentence1), len(sentence2)
// 如果长度相等,但是字符串并不相等,说明无法插入
if l1 == l2 {
return false
}
s1, s2 := strings.Split(sentence1, " "), strings.Split(sentence2, " ")
if l1 < l2 {
return match1813(s1, s2)
}
return match1813(s2, s1)

}
22 changes: 11 additions & 11 deletions leetcode/1801-1900/1813.Sentence-Similarity-III/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
sentence1, sentence2 string
expect bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "My name is Haley", "My Haley", true},
{"TestCase2", "of", "A lot of words", false},
{"TestCase3", "Eating right now", "Eating", true},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.sentence1, c.sentence2)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.sentence1, c.sentence2)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading