Skip to content

Commit 8ae8014

Browse files
committed
Add solution and test-cases for problem 2901
1 parent a01e89c commit 8ae8014

File tree

3 files changed

+93
-25
lines changed

3 files changed

+93
-25
lines changed

leetcode/2901-3000/2901.Longest-Unequal-Adjacent-Groups-Subsequence-II/README.md

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,58 @@
11
# [2901.Longest Unequal Adjacent Groups Subsequence II][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 a string array `words`, and an array `groups`, both arrays having length `n`.
5+
6+
The **hamming distance** between two strings of equal length is the number of positions at which the corresponding characters are **different**.
7+
8+
You need to select the **longest subsequence** from an array of indices `[0, 1, ..., n - 1]`, such that for the subsequence denoted as `[i0, i1, ..., ik-1]` having length `k`, the following holds:
9+
10+
- For **adjacent** indices in the subsequence, their corresponding groups are **unequal**, i.e., `groups[ij] != groups[ij+1]`, for each `j` where `0 < j + 1 < k`.
11+
- `words[ij]` and `words[ij+1]` are **equal** in length, and the **hamming distance** between them is `1`, where `0 < j + 1 < k`, for all indices in the subsequence.
12+
13+
Return a string array containing the words corresponding to the indices **(in order)** in the selected subsequence. If there are multiple answers, return any of them.
14+
15+
**Note**: strings in `words` may be **unequal** in length.
716

817
**Example 1:**
918

1019
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
20+
Input: words = ["bab","dab","cab"], groups = [1,2,2]
21+
22+
Output: ["bab","cab"]
23+
24+
Explanation: A subsequence that can be selected is [0,2].
25+
26+
groups[0] != groups[2]
27+
words[0].length == words[2].length, and the hamming distance between them is 1.
28+
So, a valid answer is [words[0],words[2]] = ["bab","cab"].
1429
15-
## 题意
16-
> ...
30+
Another subsequence that can be selected is [0,1].
1731
18-
## 题解
32+
groups[0] != groups[1]
33+
words[0].length == words[1].length, and the hamming distance between them is 1.
34+
So, another valid answer is [words[0],words[1]] = ["bab","dab"].
1935
20-
### 思路1
21-
> ...
22-
Longest Unequal Adjacent Groups Subsequence II
23-
```go
36+
It can be shown that the length of the longest subsequence of indices that satisfies the conditions is 2.
2437
```
2538

39+
**Example 2:**
40+
41+
```
42+
Input: words = ["a","b","c","d"], groups = [1,2,3,4]
43+
44+
Output: ["a","b","c","d"]
45+
46+
Explanation: We can select the subsequence [0,1,2,3].
47+
48+
It satisfies both conditions.
49+
50+
Hence, the answer is [words[0],words[1],words[2],words[3]] = ["a","b","c","d"].
51+
52+
It has the longest length among all subsequences of indices that satisfy the conditions.
53+
54+
Hence, it is the only answer.
55+
```
2656

2757
## 结语
2858

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

3-
func Solution(x bool) bool {
4-
return x
3+
func hammingDistance(a, b string) bool {
4+
if len(a) != len(b) {
5+
return false
6+
}
7+
d := 0
8+
for i := range len(a) {
9+
if a[i] != b[i] {
10+
d++
11+
if d > 1 {
12+
return false
13+
}
14+
}
15+
}
16+
return d == 1
17+
}
18+
19+
func Solution(words []string, groups []int) []string {
20+
l, index := 1, 0
21+
dp := make([][2]int, len(words))
22+
dp[0] = [2]int{1, -1} // 前一个
23+
for i := 1; i < len(words); i++ {
24+
dp[i] = [2]int{1, -1}
25+
for pre := i - 1; pre >= 0; pre-- {
26+
if groups[i] != groups[pre] && hammingDistance(words[i], words[pre]) {
27+
if dp[pre][0]+1 > dp[i][0] {
28+
dp[i] = [2]int{dp[pre][0] + 1, pre}
29+
}
30+
}
31+
}
32+
if dp[i][0] > l {
33+
l = dp[i][0]
34+
index = i
35+
}
36+
}
37+
ans := make([]string, l)
38+
for i := l - 1; i >= 0; i-- {
39+
ans[i] = words[index]
40+
index = dp[index][1]
41+
}
42+
return ans
543
}

leetcode/2901-3000/2901.Longest-Unequal-Adjacent-Groups-Subsequence-II/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+
words []string
14+
groups []int
15+
expect []string
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []string{"bab", "dab", "cab"}, []int{1, 2, 2}, []string{"bab", "dab"}},
18+
{"TestCase2", []string{"a", "b", "c", "d"}, []int{1, 2, 3, 4}, []string{"a", "b", "c", "d"}},
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.words, c.groups)
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.words, c.groups)
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)