Skip to content

Commit 1db556e

Browse files
authored
Merge pull request #230 from 0xff-dev/389
Add solution and test-cases for problem 389
2 parents ca207cf + 04d6eed commit 1db556e

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

leetcode/301-400/0389.Find-the-Difference/README.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
# [389.Find the Difference][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+
You are given two strings `s` and `t`.
5+
6+
String `t` is generated by random shuffling string `s` and then add one more letter at a random position.
7+
8+
Return the letter that was added to `t`.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: s = "abcd", t = "abcde"
14+
Output: "e"
15+
Explanation: 'e' is the letter that was added.
1316
```
1417

15-
## 题意
16-
> ...
17-
18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Find the Difference
23-
```go
18+
**Example 2:**
19+
```
20+
Input: s = "", t = "y"
21+
Output: "y"
2422
```
25-
2623

2724
## 结语
2825

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s, t string) byte {
4+
start := uint8(0)
5+
for _, b := range append([]byte(s), t...) {
6+
start ^= b
7+
}
8+
return byte(start)
59
}

leetcode/301-400/0389.Find-the-Difference/Solution_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,20 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
s, t string
14+
expect byte
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "abcd", "abcde", 'e'},
17+
{"TestCase2", "", "y", 'y'},
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.s, c.t)
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.s, c.t)
2827
}
2928
})
3029
}

0 commit comments

Comments
 (0)