Skip to content

Commit 5e475da

Browse files
authored
Merge pull request #986 from 0xff-dev/884
Add solution and test-cases for problem 884
2 parents b1a7979 + 81989ae commit 5e475da

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

leetcode/801-900/0884.Uncommon-Words-from-Two-Sentences/README.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
# [884.Uncommon Words from Two Sentences][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 string of single-space separated words where each word consists only of lowercase letters.
5+
6+
A word is **uncommon** if it appears exactly once in one of the sentences, and **does not appear** in the other sentence.
7+
8+
Given two **sentences** `s1` and `s2`, return a list of all the **uncommon words**. You may return the answer in **any order**.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
13+
Input: s1 = "this apple is sweet", s2 = "this apple is sour"
1414
15-
## 题意
16-
> ...
15+
Output: ["sweet","sour"]
1716
18-
## 题解
17+
Explanation:
1918
20-
### 思路1
21-
> ...
22-
Uncommon Words from Two Sentences
23-
```go
19+
The word "sweet" appears only in s1, while the word "sour" appears only in s2.
2420
```
2521

22+
**Example 2:**
23+
24+
```
25+
Input: s1 = "apple apple", s2 = "banana"
26+
27+
Output: ["banana"]
28+
```
2629

2730
## 结语
2831

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
func Solution(s1 string, s2 string) []string {
6+
ans := make([]string, 0)
7+
ls1 := strings.Split(s1, " ")
8+
ls2 := strings.Split(s2, " ")
9+
in1, in2 := make(map[string]int), make(map[string]int)
10+
for _, w := range ls1 {
11+
in1[w]++
12+
}
13+
for _, w := range ls2 {
14+
in2[w]++
15+
}
16+
for w, c := range in1 {
17+
if c != 1 {
18+
continue
19+
}
20+
if _, ok := in2[w]; !ok {
21+
ans = append(ans, w)
22+
}
23+
}
24+
for w, c := range in2 {
25+
if c != 1 {
26+
continue
27+
}
28+
if _, ok := in1[w]; !ok {
29+
ans = append(ans, w)
30+
}
31+
}
32+
return ans
533
}

leetcode/801-900/0884.Uncommon-Words-from-Two-Sentences/Solution_test.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,29 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
s1, s2 string
14+
expect []string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "this apple is sweet", "this apple is sour", []string{"sweet", "sour"}},
17+
{"TestCase2", "apple apple", "banana", []string{"banana"}},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.s1, c.s2)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.s1, c.s2)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)