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 884 #986

Merged
merged 1 commit into from
Sep 20, 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
29 changes: 16 additions & 13 deletions leetcode/801-900/0884.Uncommon-Words-from-Two-Sentences/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
# [884.Uncommon Words from Two Sentences][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 string of single-space separated words where each word consists only of lowercase letters.

A word is **uncommon** if it appears exactly once in one of the sentences, and **does not appear** in the other sentence.

Given two **sentences** `s1` and `s2`, return a list of all the **uncommon words**. You may return the answer in **any order**.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Input: s1 = "this apple is sweet", s2 = "this apple is sour"

## 题意
> ...
Output: ["sweet","sour"]

## 题解
Explanation:

### 思路1
> ...
Uncommon Words from Two Sentences
```go
The word "sweet" appears only in s1, while the word "sour" appears only in s2.
```

**Example 2:**

```
Input: s1 = "apple apple", s2 = "banana"

Output: ["banana"]
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
package Solution

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

func Solution(s1 string, s2 string) []string {
ans := make([]string, 0)
ls1 := strings.Split(s1, " ")
ls2 := strings.Split(s2, " ")
in1, in2 := make(map[string]int), make(map[string]int)
for _, w := range ls1 {
in1[w]++
}
for _, w := range ls2 {
in2[w]++
}
for w, c := range in1 {
if c != 1 {
continue
}
if _, ok := in2[w]; !ok {
ans = append(ans, w)
}
}
for w, c := range in2 {
if c != 1 {
continue
}
if _, ok := in1[w]; !ok {
ans = append(ans, w)
}
}
return ans
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
s1, s2 string
expect []string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "this apple is sweet", "this apple is sour", []string{"sweet", "sour"}},
{"TestCase2", "apple apple", "banana", []string{"banana"}},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.s1, c.s2)
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.s1, c.s2)
}
})
}
}

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

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