Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit de7f345

Browse files
committed
Merge pull request #377 from 0xff-dev/1143
Add solution and test-cases for problem 1143
2 parents f8cd8c4 + 0337029 commit de7f345

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

‎leetcode/1101-1200/1143.Longest-Common-Subsequence/README.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [1143.Longest Common 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+
Given two strings `text1` and `text2`, return the length of their longest **common subsequence**. If there is no **common subsequence**, return `0`.
5+
6+
A `subsequence` of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
7+
8+
- For example, `"ace"` is a subsequence of `"abcde"`.
9+
10+
A **common subsequence** of two strings is a subsequence that is common to both strings.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: text1 = "abcde", text2 = "ace"
16+
Output: 3
17+
Explanation: The longest common subsequence is "ace" and its length is 3.
1318
```
1419

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

20-
### 思路1
21-
> ...
22-
Longest Common Subsequence
23-
```go
2422
```
23+
Input: text1 = "abc", text2 = "abc"
24+
Output: 3
25+
Explanation: The longest common subsequence is "abc" and its length is 3.
26+
```
27+
28+
**Example 3:**
2529

30+
```
31+
Input: text1 = "abc", text2 = "def"
32+
Output: 0
33+
Explanation: There is no such common subsequence, so the result is 0.
34+
```
2635

2736
## 结语
2837

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(text1 string, text2 string) int {
4+
l1, l2 := len(text1), len(text2)
5+
dp := make([][]int, 2)
6+
for idx := 0; idx < 2; idx++ {
7+
dp[idx] = make([]int, l2+1)
8+
}
9+
loop := 1
10+
for row := 0; row < l1; row++ {
11+
for col := 1; col <= l2; col++ {
12+
if text1[row] == text2[col-1] {
13+
dp[loop][col] = dp[1-loop][col-1] + 1
14+
continue
15+
}
16+
_max := dp[1-loop][col]
17+
if dp[loop][col-1] > _max {
18+
_max = dp[loop][col-1]
19+
}
20+
dp[loop][col] = _max
21+
}
22+
23+
loop = 1 - loop
24+
}
25+
return dp[1-loop][l2]
526
}

‎leetcode/1101-1200/1143.Longest-Common-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-
inputsbool
14-
expect bool
13+
t1, t2string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "abcde", "acd", 3},
17+
{"TestCase2", "abc", "abc", 3},
18+
{"TestCase3", "abc", "def", 0},
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.t1, c.t2)
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.t1, c.t2)
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)

AltStyle によって変換されたページ (->オリジナル) /