From 1ccacfa81cc024c897c9e5a1d78e44035167c32c Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 15 May 2025 06:26:49 +0800 Subject: [PATCH 1/2] feat: update solutions to lc problems: No.2900,2901 --- .../README.md | 44 ++++----- .../README_EN.md | 44 ++++----- .../Solution.cpp | 3 +- .../Solution.go | 14 +-- .../Solution.java | 3 +- .../Solution.py | 4 +- .../Solution.rs | 16 ++-- .../Solution.ts | 4 +- .../README.md | 89 +++---------------- .../README_EN.md | 89 +++---------------- .../Solution.cpp | 3 +- .../Solution.go | 7 +- .../Solution.java | 3 +- .../Solution.py | 3 +- .../Solution.rs | 8 +- .../Solution.ts | 3 +- .../Solution2.java | 45 ---------- 17 files changed, 96 insertions(+), 286 deletions(-) delete mode 100644 solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution2.java diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md index b48e43453d133..c25600ff70fe0 100644 --- a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md @@ -87,9 +87,7 @@ tags: ```python class Solution: - def getWordsInLongestSubsequence( - self, n: int, words: List[str], groups: List[int] - ) -> List[str]: + def getLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]: return [words[i] for i, x in enumerate(groups) if i == 0 or x != groups[i - 1]] ``` @@ -97,7 +95,8 @@ class Solution: ```java class Solution { - public List getWordsInLongestSubsequence(int n, String[] words, int[] groups) { + public List getLongestSubsequence(String[] words, int[] groups) { + int n = groups.length; List ans = new ArrayList<>(); for (int i = 0; i < n; ++i) { if (i == 0 || groups[i] != groups[i - 1]) { @@ -114,7 +113,8 @@ class Solution { ```cpp class Solution { public: - vector getWordsInLongestSubsequence(int n, vector& words, vector& groups) { + vector getLongestSubsequence(vector& words, vector& groups) { + int n = groups.size(); vector ans; for (int i = 0; i < n; ++i) { if (i == 0 || groups[i] != groups[i - 1]) { @@ -129,22 +129,22 @@ public: #### Go ```go -func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []string) { - for i, x := range groups { - if i == 0 || x != groups[i-1] { - ans = append(ans, words[i]) - } - } - return +func getLongestSubsequence(words []string, groups []int) (ans []string) { + for i, x := range groups { + if i == 0 || x != groups[i - 1] { + ans = append(ans, words[i]) + } + } + return } ``` #### TypeScript ```ts -function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] { +function getLongestSubsequence(words: string[], groups: number[]): string[] { const ans: string[] = []; - for (let i = 0; i < n; ++i) { + for (let i = 0; i < groups.length; ++i) { if (i === 0 || groups[i] !== groups[i - 1]) { ans.push(words[i]); } @@ -157,19 +157,13 @@ function getWordsInLongestSubsequence(n: number, words: string[], groups: number ```rust impl Solution { - pub fn get_words_in_longest_subsequence( - n: i32, - words: Vec, - groups: Vec, - ) -> Vec { - let mut ans = vec![]; - - for i in 0..n { - if i == 0 || groups[i as usize] != groups[(i - 1) as usize] { - ans.push(words[i as usize].clone()); + pub fn get_longest_subsequence(words: Vec, groups: Vec) -> Vec { + let mut ans = Vec::new(); + for (i, &g) in groups.iter().enumerate() { + if i == 0 || g != groups[i - 1] { + ans.push(words[i].clone()); } } - ans } } diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md index 913cdda134f56..5568dc9e3075a 100644 --- a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md @@ -111,9 +111,7 @@ The time complexity is $O(n)$, where $n$ is the length of the array $groups$. Th ```python class Solution: - def getWordsInLongestSubsequence( - self, n: int, words: List[str], groups: List[int] - ) -> List[str]: + def getLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]: return [words[i] for i, x in enumerate(groups) if i == 0 or x != groups[i - 1]] ``` @@ -121,7 +119,8 @@ class Solution: ```java class Solution { - public List getWordsInLongestSubsequence(int n, String[] words, int[] groups) { + public List getLongestSubsequence(String[] words, int[] groups) { + int n = groups.length; List ans = new ArrayList<>(); for (int i = 0; i < n; ++i) { if (i == 0 || groups[i] != groups[i - 1]) { @@ -138,7 +137,8 @@ class Solution { ```cpp class Solution { public: - vector getWordsInLongestSubsequence(int n, vector& words, vector& groups) { + vector getLongestSubsequence(vector& words, vector& groups) { + int n = groups.size(); vector ans; for (int i = 0; i < n; ++i) { if (i == 0 || groups[i] != groups[i - 1]) { @@ -153,22 +153,22 @@ public: #### Go ```go -func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []string) { - for i, x := range groups { - if i == 0 || x != groups[i-1] { - ans = append(ans, words[i]) - } - } - return +func getLongestSubsequence(words []string, groups []int) (ans []string) { + for i, x := range groups { + if i == 0 || x != groups[i - 1] { + ans = append(ans, words[i]) + } + } + return } ``` #### TypeScript ```ts -function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] { +function getLongestSubsequence(words: string[], groups: number[]): string[] { const ans: string[] = []; - for (let i = 0; i < n; ++i) { + for (let i = 0; i < groups.length; ++i) { if (i === 0 || groups[i] !== groups[i - 1]) { ans.push(words[i]); } @@ -181,19 +181,13 @@ function getWordsInLongestSubsequence(n: number, words: string[], groups: number ```rust impl Solution { - pub fn get_words_in_longest_subsequence( - n: i32, - words: Vec, - groups: Vec, - ) -> Vec { - let mut ans = vec![]; - - for i in 0..n { - if i == 0 || groups[i as usize] != groups[(i - 1) as usize] { - ans.push(words[i as usize].clone()); + pub fn get_longest_subsequence(words: Vec, groups: Vec) -> Vec { + let mut ans = Vec::new(); + for (i, &g) in groups.iter().enumerate() { + if i == 0 || g != groups[i - 1] { + ans.push(words[i].clone()); } } - ans } } diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.cpp b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.cpp index c686de7e6c4d1..177cf71800f51 100644 --- a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.cpp +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.cpp @@ -1,6 +1,7 @@ class Solution { public: - vector getWordsInLongestSubsequence(int n, vector& words, vector& groups) { + vector getLongestSubsequence(vector& words, vector& groups) { + int n = groups.size(); vector ans; for (int i = 0; i < n; ++i) { if (i == 0 || groups[i] != groups[i - 1]) { diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.go b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.go index 0fbd97a384c83..2f0075d471e79 100644 --- a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.go +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.go @@ -1,8 +1,8 @@ -func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []string) { - for i, x := range groups { - if i == 0 || x != groups[i-1] { - ans = append(ans, words[i]) - } - } - return +func getLongestSubsequence(words []string, groups []int) (ans []string) { + for i, x := range groups { + if i == 0 || x != groups[i - 1] { + ans = append(ans, words[i]) + } + } + return } \ No newline at end of file diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.java b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.java index 9e3b87d016e6d..4427ba33b47cf 100644 --- a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.java +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.java @@ -1,5 +1,6 @@ class Solution { - public List getWordsInLongestSubsequence(int n, String[] words, int[] groups) { + public List getLongestSubsequence(String[] words, int[] groups) { + int n = groups.length; List ans = new ArrayList<>(); for (int i = 0; i < n; ++i) { if (i == 0 || groups[i] != groups[i - 1]) { diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.py b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.py index 250331a458490..bde890e899558 100644 --- a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.py +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.py @@ -1,5 +1,3 @@ class Solution: - def getWordsInLongestSubsequence( - self, n: int, words: List[str], groups: List[int] - ) -> List[str]: + def getLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]: return [words[i] for i, x in enumerate(groups) if i == 0 or x != groups[i - 1]] diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.rs b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.rs index 66c780a2540ba..66b874fa2b1f8 100644 --- a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.rs +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.rs @@ -1,17 +1,11 @@ impl Solution { - pub fn get_words_in_longest_subsequence( - n: i32, - words: Vec, - groups: Vec, - ) -> Vec { - let mut ans = vec![]; - - for i in 0..n { - if i == 0 || groups[i as usize] != groups[(i - 1) as usize] { - ans.push(words[i as usize].clone()); + pub fn get_longest_subsequence(words: Vec, groups: Vec) -> Vec { + let mut ans = Vec::new(); + for (i, &g) in groups.iter().enumerate() { + if i == 0 || g != groups[i - 1] { + ans.push(words[i].clone()); } } - ans } } diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.ts b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.ts index 71386aa0ab21c..b59277df43595 100644 --- a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.ts +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.ts @@ -1,6 +1,6 @@ -function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] { +function getLongestSubsequence(words: string[], groups: number[]): string[] { const ans: string[] = []; - for (let i = 0; i < n; ++i) { + for (let i = 0; i < groups.length; ++i) { if (i === 0 || groups[i] !== groups[i - 1]) { ans.push(words[i]); } diff --git a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README.md b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README.md index 4758e78c64610..6bf0b690cf9be 100644 --- a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README.md +++ b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README.md @@ -103,11 +103,12 @@ tags: ```python class Solution: def getWordsInLongestSubsequence( - self, n: int, words: List[str], groups: List[int] + self, words: List[str], groups: List[int] ) -> List[str]: def check(s: str, t: str) -> bool: return len(s) == len(t) and sum(a != b for a, b in zip(s, t)) == 1 + n = len(groups) f = [1] * n g = [-1] * n mx = 1 @@ -132,7 +133,8 @@ class Solution: ```java class Solution { - public List getWordsInLongestSubsequence(int n, String[] words, int[] groups) { + public List getWordsInLongestSubsequence(String[] words, int[] groups) { + int n = groups.length; int[] f = new int[n]; int[] g = new int[n]; Arrays.fill(f, 1); @@ -180,7 +182,7 @@ class Solution { ```cpp class Solution { public: - vector getWordsInLongestSubsequence(int n, vector& words, vector& groups) { + vector getWordsInLongestSubsequence(vector& words, vector& groups) { auto check = [](string& s, string& t) { if (s.size() != t.size()) { return false; @@ -191,6 +193,7 @@ public: } return cnt == 1; }; + int n = groups.size(); vector f(n, 1); vector g(n, -1); int mx = 1; @@ -221,7 +224,7 @@ public: #### Go ```go -func getWordsInLongestSubsequence(n int, words []string, groups []int) []string { +func getWordsInLongestSubsequence(words []string, groups []int) []string { check := func(s, t string) bool { if len(s) != len(t) { return false @@ -234,6 +237,7 @@ func getWordsInLongestSubsequence(n int, words []string, groups []int) []string } return cnt == 1 } + n := len(groups) f := make([]int, n) g := make([]int, n) for i := range f { @@ -261,9 +265,7 @@ func getWordsInLongestSubsequence(n int, words []string, groups []int) []string break } } - for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 { - ans[i], ans[j] = ans[j], ans[i] - } + slices.Reverse(ans) return ans } ``` @@ -271,7 +273,8 @@ func getWordsInLongestSubsequence(n int, words []string, groups []int) []string #### TypeScript ```ts -function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] { +function getWordsInLongestSubsequence(words: string[], groups: number[]): string[] { + const n = groups.length; const f: number[] = Array(n).fill(1); const g: number[] = Array(n).fill(-1); let mx = 1; @@ -313,16 +316,12 @@ function getWordsInLongestSubsequence(n: number, words: string[], groups: number ```rust impl Solution { - pub fn get_words_in_longest_subsequence( - n: i32, - words: Vec, - groups: Vec, - ) -> Vec { + pub fn get_words_in_longest_subsequence(words: Vec, groups: Vec) -> Vec { fn check(s: &str, t: &str) -> bool { s.len() == t.len() && s.chars().zip(t.chars()).filter(|(a, b)| a != b).count() == 1 } - let n = n as usize; + let n = groups.len(); let mut f = vec![1; n]; let mut g = vec![-1; n]; @@ -364,66 +363,4 @@ impl Solution { - - -### 方法二:动态规划 + 通配符哈希表 - -在方法一中,我们需要枚举所有的 $i$ 和 $j$ 组合, 这一步可以通过维护一个通配符哈希表来优化. 对于每个字符串 $word[i]$, 我们枚举它的每个字符, 将其替换为通配符, 然后将替换后的字符串作为键, 将其下标作为值存入哈希表中. 这样我们可以在 $O(L)$ 时间内找到所有距离 $word[i]$ 汉明距离为 1 的 $word[j]$. 尽管时间复杂度仍然是 $O(n^2 \times L)$, 但平均复杂度会有所降低. - - - -#### Java - -```java -class Solution { - public List getWordsInLongestSubsequence(int n, String[] words, int[] groups) { - int[] dp = new int[n]; - int[] next = new int[n]; - Map> strToIdxMap = new HashMap<>(); - int maxIdx = n; - for (int i = n - 1; i >= 0; i--) { - int prevIdx = n; - char[] word = words[i].toCharArray(); - for (int j = 0; j < word.length; j++) { - // convert word to pattern with '*'. - char temp = word[j]; - word[j] = '*'; - String curr = new String(word); - - // search matches and update dp. - List prevList = strToIdxMap.getOrDefault(curr, List.of()); - for (int prev : prevList) { - if (groups[prev] == groups[i] || dp[prev] < dp[i]) { - continue; - } - dp[i] = dp[prev] + 1; - prevIdx = prev; - } - - // append current pattern to dictionary. - strToIdxMap.computeIfAbsent(curr, k -> new ArrayList<>()).add(i); - - // restore pattern to orignal word. - word[j] = temp; - } - if (maxIdx >= n || dp[i] > dp[maxIdx]) { - maxIdx = i; - } - next[i] = prevIdx; - } - int curr = maxIdx; - List ans = new ArrayList<>(); - while (curr < n) { - ans.add(words[curr]); - curr = next[curr]; - } - return ans; - } -} -``` - - - - - diff --git a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README_EN.md b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README_EN.md index 3e2d53f6701c4..aa13c6ce76010 100644 --- a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README_EN.md +++ b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README_EN.md @@ -118,11 +118,12 @@ The time complexity is $O(n^2 \times L)$, and the space complexity is $O(n)$. He ```python class Solution: def getWordsInLongestSubsequence( - self, n: int, words: List[str], groups: List[int] + self, words: List[str], groups: List[int] ) -> List[str]: def check(s: str, t: str) -> bool: return len(s) == len(t) and sum(a != b for a, b in zip(s, t)) == 1 + n = len(groups) f = [1] * n g = [-1] * n mx = 1 @@ -147,7 +148,8 @@ class Solution: ```java class Solution { - public List getWordsInLongestSubsequence(int n, String[] words, int[] groups) { + public List getWordsInLongestSubsequence(String[] words, int[] groups) { + int n = groups.length; int[] f = new int[n]; int[] g = new int[n]; Arrays.fill(f, 1); @@ -195,7 +197,7 @@ class Solution { ```cpp class Solution { public: - vector getWordsInLongestSubsequence(int n, vector& words, vector& groups) { + vector getWordsInLongestSubsequence(vector& words, vector& groups) { auto check = [](string& s, string& t) { if (s.size() != t.size()) { return false; @@ -206,6 +208,7 @@ public: } return cnt == 1; }; + int n = groups.size(); vector f(n, 1); vector g(n, -1); int mx = 1; @@ -236,7 +239,7 @@ public: #### Go ```go -func getWordsInLongestSubsequence(n int, words []string, groups []int) []string { +func getWordsInLongestSubsequence(words []string, groups []int) []string { check := func(s, t string) bool { if len(s) != len(t) { return false @@ -249,6 +252,7 @@ func getWordsInLongestSubsequence(n int, words []string, groups []int) []string } return cnt == 1 } + n := len(groups) f := make([]int, n) g := make([]int, n) for i := range f { @@ -276,9 +280,7 @@ func getWordsInLongestSubsequence(n int, words []string, groups []int) []string break } } - for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 { - ans[i], ans[j] = ans[j], ans[i] - } + slices.Reverse(ans) return ans } ``` @@ -286,7 +288,8 @@ func getWordsInLongestSubsequence(n int, words []string, groups []int) []string #### TypeScript ```ts -function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] { +function getWordsInLongestSubsequence(words: string[], groups: number[]): string[] { + const n = groups.length; const f: number[] = Array(n).fill(1); const g: number[] = Array(n).fill(-1); let mx = 1; @@ -328,16 +331,12 @@ function getWordsInLongestSubsequence(n: number, words: string[], groups: number ```rust impl Solution { - pub fn get_words_in_longest_subsequence( - n: i32, - words: Vec, - groups: Vec, - ) -> Vec { + pub fn get_words_in_longest_subsequence(words: Vec, groups: Vec) -> Vec { fn check(s: &str, t: &str) -> bool { s.len() == t.len() && s.chars().zip(t.chars()).filter(|(a, b)| a != b).count() == 1 } - let n = n as usize; + let n = groups.len(); let mut f = vec![1; n]; let mut g = vec![-1; n]; @@ -379,66 +378,4 @@ impl Solution { - - -### Solution 2: Dynamic Programming + Hash Table - -In Solution 1, we need to enumerate all $i$ and $j$ combinations, a step that can be optimized by maintaining a wildcard hash table. For each string $word[i]$, we enumerate each character, replace it with a wildcard, and then use the replaced string as the key and add its subscript to the list which is the value in the hash table. This allows us to find all $word[j]$ with a Hamming distance of 1 from $word[i]$ in $O(L)$ time. Although the time complexity is still $O(n^2 \times L)$, the average complexity is reduced. - - - -#### Java - -```java -class Solution { - public List getWordsInLongestSubsequence(int n, String[] words, int[] groups) { - int[] dp = new int[n]; - int[] next = new int[n]; - Map> strToIdxMap = new HashMap<>(); - int maxIdx = n; - for (int i = n - 1; i >= 0; i--) { - int prevIdx = n; - char[] word = words[i].toCharArray(); - for (int j = 0; j < word.length; j++) { - // convert word to pattern with '*'. - char temp = word[j]; - word[j] = '*'; - String curr = new String(word); - - // search matches and update dp. - List prevList = strToIdxMap.getOrDefault(curr, List.of()); - for (int prev : prevList) { - if (groups[prev] == groups[i] || dp[prev] < dp[i]) { - continue; - } - dp[i] = dp[prev] + 1; - prevIdx = prev; - } - - // append current pattern to dictionary. - strToIdxMap.computeIfAbsent(curr, k -> new ArrayList<>()).add(i); - - // restore pattern to orignal word. - word[j] = temp; - } - if (maxIdx >= n || dp[i] > dp[maxIdx]) { - maxIdx = i; - } - next[i] = prevIdx; - } - int curr = maxIdx; - List ans = new ArrayList<>(); - while (curr < n) { - ans.add(words[curr]); - curr = next[curr]; - } - return ans; - } -} -``` - - - - - diff --git a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.cpp b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.cpp index 97ee1a7627352..f761526b8aabb 100644 --- a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.cpp +++ b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.cpp @@ -1,6 +1,6 @@ class Solution { public: - vector getWordsInLongestSubsequence(int n, vector& words, vector& groups) { + vector getWordsInLongestSubsequence(vector& words, vector& groups) { auto check = [](string& s, string& t) { if (s.size() != t.size()) { return false; @@ -11,6 +11,7 @@ class Solution { } return cnt == 1; }; + int n = groups.size(); vector f(n, 1); vector g(n, -1); int mx = 1; diff --git a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.go b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.go index 54f7f6e5de467..5b698e8e736e3 100644 --- a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.go +++ b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.go @@ -1,4 +1,4 @@ -func getWordsInLongestSubsequence(n int, words []string, groups []int) []string { +func getWordsInLongestSubsequence(words []string, groups []int) []string { check := func(s, t string) bool { if len(s) != len(t) { return false @@ -11,6 +11,7 @@ func getWordsInLongestSubsequence(n int, words []string, groups []int) []string } return cnt == 1 } + n := len(groups) f := make([]int, n) g := make([]int, n) for i := range f { @@ -38,8 +39,6 @@ func getWordsInLongestSubsequence(n int, words []string, groups []int) []string break } } - for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 { - ans[i], ans[j] = ans[j], ans[i] - } + slices.Reverse(ans) return ans } \ No newline at end of file diff --git a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.java b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.java index b105a52b10cb2..4e9ce1df60a2e 100644 --- a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.java +++ b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.java @@ -1,5 +1,6 @@ class Solution { - public List getWordsInLongestSubsequence(int n, String[] words, int[] groups) { + public List getWordsInLongestSubsequence(String[] words, int[] groups) { + int n = groups.length; int[] f = new int[n]; int[] g = new int[n]; Arrays.fill(f, 1); diff --git a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.py b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.py index 6ebef77cca98a..b02d4a4980ed3 100644 --- a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.py +++ b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.py @@ -1,10 +1,11 @@ class Solution: def getWordsInLongestSubsequence( - self, n: int, words: List[str], groups: List[int] + self, words: List[str], groups: List[int] ) -> List[str]: def check(s: str, t: str) -> bool: return len(s) == len(t) and sum(a != b for a, b in zip(s, t)) == 1 + n = len(groups) f = [1] * n g = [-1] * n mx = 1 diff --git a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.rs b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.rs index 774bb25bbbfae..bc64b4297eb90 100644 --- a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.rs +++ b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.rs @@ -1,14 +1,10 @@ impl Solution { - pub fn get_words_in_longest_subsequence( - n: i32, - words: Vec, - groups: Vec, - ) -> Vec { + pub fn get_words_in_longest_subsequence(words: Vec, groups: Vec) -> Vec { fn check(s: &str, t: &str) -> bool { s.len() == t.len() && s.chars().zip(t.chars()).filter(|(a, b)| a != b).count() == 1 } - let n = n as usize; + let n = groups.len(); let mut f = vec![1; n]; let mut g = vec![-1; n]; diff --git a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.ts b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.ts index 63d9dc9ad9a0b..5aaa30718b002 100644 --- a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.ts +++ b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution.ts @@ -1,4 +1,5 @@ -function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] { +function getWordsInLongestSubsequence(words: string[], groups: number[]): string[] { + const n = groups.length; const f: number[] = Array(n).fill(1); const g: number[] = Array(n).fill(-1); let mx = 1; diff --git a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution2.java b/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution2.java deleted file mode 100644 index 60a36fe7e9627..0000000000000 --- a/solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/Solution2.java +++ /dev/null @@ -1,45 +0,0 @@ -class Solution { - public List getWordsInLongestSubsequence(int n, String[] words, int[] groups) { - int[] dp = new int[n]; - int[] next = new int[n]; - Map> strToIdxMap = new HashMap<>(); - int maxIdx = n; - for (int i = n - 1; i >= 0; i--) { - int prevIdx = n; - char[] word = words[i].toCharArray(); - for (int j = 0; j < word.length; j++) { - // convert word to pattern with '*'. - char temp = word[j]; - word[j] = '*'; - String curr = new String(word); - - // search matches and update dp. - List prevList = strToIdxMap.getOrDefault(curr, List.of()); - for (int prev : prevList) { - if (groups[prev] == groups[i] || dp[prev] < dp[i]) { - continue; - } - dp[i] = dp[prev] + 1; - prevIdx = prev; - } - - // append current pattern to dictionary. - strToIdxMap.computeIfAbsent(curr, k -> new ArrayList<>()).add(i); - - // restore pattern to orignal word. - word[j] = temp; - } - if (maxIdx >= n || dp[i] > dp[maxIdx]) { - maxIdx = i; - } - next[i] = prevIdx; - } - int curr = maxIdx; - List ans = new ArrayList<>(); - while (curr < n) { - ans.add(words[curr]); - curr = next[curr]; - } - return ans; - } -} \ No newline at end of file From 190b082451429caab5033b10c99b5ad383cac833 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Thu, 15 May 2025 06:31:06 +0800 Subject: [PATCH 2/2] fix: go code --- .../README.md | 12 ++++++------ .../README_EN.md | 12 ++++++------ .../Solution.go | 12 ++++++------ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md index c25600ff70fe0..b51abc75decc7 100644 --- a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md @@ -130,12 +130,12 @@ public: ```go func getLongestSubsequence(words []string, groups []int) (ans []string) { - for i, x := range groups { - if i == 0 || x != groups[i - 1] { - ans = append(ans, words[i]) - } - } - return + for i, x := range groups { + if i == 0 || x != groups[i-1] { + ans = append(ans, words[i]) + } + } + return } ``` diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md index 5568dc9e3075a..55e22c206e9de 100644 --- a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md @@ -154,12 +154,12 @@ public: ```go func getLongestSubsequence(words []string, groups []int) (ans []string) { - for i, x := range groups { - if i == 0 || x != groups[i - 1] { - ans = append(ans, words[i]) - } - } - return + for i, x := range groups { + if i == 0 || x != groups[i-1] { + ans = append(ans, words[i]) + } + } + return } ``` diff --git a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.go b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.go index 2f0075d471e79..6acc292202785 100644 --- a/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.go +++ b/solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.go @@ -1,8 +1,8 @@ func getLongestSubsequence(words []string, groups []int) (ans []string) { - for i, x := range groups { - if i == 0 || x != groups[i - 1] { - ans = append(ans, words[i]) - } - } - return + for i, x := range groups { + if i == 0 || x != groups[i-1] { + ans = append(ans, words[i]) + } + } + return } \ No newline at end of file