diff --git a/leetcode/2901-3000/2901.Longest-Unequal-Adjacent-Groups-Subsequence-II/README.md b/leetcode/2901-3000/2901.Longest-Unequal-Adjacent-Groups-Subsequence-II/README.md index 801bd5a44..67f9ae835 100755 --- a/leetcode/2901-3000/2901.Longest-Unequal-Adjacent-Groups-Subsequence-II/README.md +++ b/leetcode/2901-3000/2901.Longest-Unequal-Adjacent-Groups-Subsequence-II/README.md @@ -1,28 +1,58 @@ # [2901.Longest Unequal Adjacent Groups Subsequence II][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given a string array `words`, and an array `groups`, both arrays having length `n`. + +The **hamming distance** between two strings of equal length is the number of positions at which the corresponding characters are **different**. + +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: + +- 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`. +- `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. + +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. + +**Note**: strings in `words` may be **unequal** in length. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: words = ["bab","dab","cab"], groups = [1,2,2] + +Output: ["bab","cab"] + +Explanation: A subsequence that can be selected is [0,2]. + +groups[0] != groups[2] +words[0].length == words[2].length, and the hamming distance between them is 1. +So, a valid answer is [words[0],words[2]] = ["bab","cab"]. -## 题意 -> ... +Another subsequence that can be selected is [0,1]. -## 题解 +groups[0] != groups[1] +words[0].length == words[1].length, and the hamming distance between them is 1. +So, another valid answer is [words[0],words[1]] = ["bab","dab"]. -### 思路1 -> ... -Longest Unequal Adjacent Groups Subsequence II -```go +It can be shown that the length of the longest subsequence of indices that satisfies the conditions is 2. ``` +**Example 2:** + +``` +Input: words = ["a","b","c","d"], groups = [1,2,3,4] + +Output: ["a","b","c","d"] + +Explanation: We can select the subsequence [0,1,2,3]. + +It satisfies both conditions. + +Hence, the answer is [words[0],words[1],words[2],words[3]] = ["a","b","c","d"]. + +It has the longest length among all subsequences of indices that satisfy the conditions. + +Hence, it is the only answer. +``` ## 结语 diff --git a/leetcode/2901-3000/2901.Longest-Unequal-Adjacent-Groups-Subsequence-II/Solution.go b/leetcode/2901-3000/2901.Longest-Unequal-Adjacent-Groups-Subsequence-II/Solution.go index d115ccf5e..a9fe40ebd 100644 --- a/leetcode/2901-3000/2901.Longest-Unequal-Adjacent-Groups-Subsequence-II/Solution.go +++ b/leetcode/2901-3000/2901.Longest-Unequal-Adjacent-Groups-Subsequence-II/Solution.go @@ -1,5 +1,43 @@ package Solution -func Solution(x bool) bool { - return x +func hammingDistance(a, b string) bool { + if len(a) != len(b) { + return false + } + d := 0 + for i := range len(a) { + if a[i] != b[i] { + d++ + if d > 1 { + return false + } + } + } + return d == 1 +} + +func Solution(words []string, groups []int) []string { + l, index := 1, 0 + dp := make([][2]int, len(words)) + dp[0] = [2]int{1, -1} // 前一个 + for i := 1; i < len(words); i++ { + dp[i] = [2]int{1, -1} + for pre := i - 1; pre >= 0; pre-- { + if groups[i] != groups[pre] && hammingDistance(words[i], words[pre]) { + if dp[pre][0]+1 > dp[i][0] { + dp[i] = [2]int{dp[pre][0] + 1, pre} + } + } + } + if dp[i][0] > l { + l = dp[i][0] + index = i + } + } + ans := make([]string, l) + for i := l - 1; i >= 0; i-- { + ans[i] = words[index] + index = dp[index][1] + } + return ans } diff --git a/leetcode/2901-3000/2901.Longest-Unequal-Adjacent-Groups-Subsequence-II/Solution_test.go b/leetcode/2901-3000/2901.Longest-Unequal-Adjacent-Groups-Subsequence-II/Solution_test.go index 14ff50eb4..b2a40fe11 100644 --- a/leetcode/2901-3000/2901.Longest-Unequal-Adjacent-Groups-Subsequence-II/Solution_test.go +++ b/leetcode/2901-3000/2901.Longest-Unequal-Adjacent-Groups-Subsequence-II/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + words []string + groups []int + expect []string }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []string{"bab", "dab", "cab"}, []int{1, 2, 2}, []string{"bab", "dab"}}, + {"TestCase2", []string{"a", "b", "c", "d"}, []int{1, 2, 3, 4}, []string{"a", "b", "c", "d"}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.words, c.groups) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.words, c.groups) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }