Skip to content

Commit d4251e0

Browse files
authored
Merge pull request #893 from 0xff-dev/2486
Add solution and test-cases for problem 2486
2 parents e35d540 + 7ad68f8 commit d4251e0

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

leetcode/2401-2500/2486.Append-Characters-to-String-to-Make-Subsequence/README.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [2486.Append Characters to String to Make Subsequence][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` consisting of only lowercase English letters.
5+
6+
Return the minimum number of characters that need to be appended to the end of `s` so that `t` becomes a **subsequence** of `s`.
7+
8+
A **subsequence** is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: s = "coaching", t = "coding"
14+
Output: 4
15+
Explanation: Append the characters "ding" to the end of s so that s = "coachingding".
16+
Now, t is a subsequence of s ("coachingding").
17+
It can be shown that appending any 3 characters to the end of s will never make t a subsequence.
1318
```
1419

15-
## 题意
16-
> ...
17-
18-
## 题解
20+
**Example 2:**
1921

20-
### 思路1
21-
> ...
22-
Append Characters to String to Make Subsequence
23-
```go
2422
```
23+
Input: s = "abcde", t = "a"
24+
Output: 0
25+
Explanation: t is already a subsequence of s ("abcde").
26+
```
27+
28+
**Example 3:**
2529

30+
```
31+
Input: s = "z", t = "abcde"
32+
Output: 5
33+
Explanation: Append the characters "abcde" to the end of s so that s = "zabcde".
34+
Now, t is a subsequence of s ("zabcde").
35+
It can be shown that appending any 4 characters to the end of s will never make t a subsequence.
36+
```
2637

2738
## 结语
2839

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string, t string) int {
4+
match := 0
5+
i, j := 0, 0
6+
for i < len(s) && j < len(t) {
7+
if s[i] == t[j] {
8+
match++
9+
i++
10+
j++
11+
continue
12+
}
13+
i++
14+
}
15+
return len(t) - match
516
}

leetcode/2401-2500/2486.Append-Characters-to-String-to-Make-Subsequence/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
ss, tt string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "coaching", "coding", 4},
17+
{"TestCase2", "abcde", "a", 0},
18+
{"TestCase3", "z", "abcde", 5},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.ss, c.tt)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.ss, c.tt)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)