Skip to content

Commit 52f89c8

Browse files
authored
feat: update solutions to lc problems: No.2900,2901 (#4408)
1 parent b6b8817 commit 52f89c8

File tree

17 files changed

+78
-268
lines changed

17 files changed

+78
-268
lines changed

solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README.md

+13-19
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,16 @@ tags:
8787

8888
```python
8989
class Solution:
90-
def getWordsInLongestSubsequence(
91-
self, n: int, words: List[str], groups: List[int]
92-
) -> List[str]:
90+
def getLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]:
9391
return [words[i] for i, x in enumerate(groups) if i == 0 or x != groups[i - 1]]
9492
```
9593

9694
#### Java
9795

9896
```java
9997
class Solution {
100-
public List<String> getWordsInLongestSubsequence(int n, String[] words, int[] groups) {
98+
public List<String> getLongestSubsequence(String[] words, int[] groups) {
99+
int n = groups.length;
101100
List<String> ans = new ArrayList<>();
102101
for (int i = 0; i < n; ++i) {
103102
if (i == 0 || groups[i] != groups[i - 1]) {
@@ -114,7 +113,8 @@ class Solution {
114113
```cpp
115114
class Solution {
116115
public:
117-
vector<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
116+
vector<string> getLongestSubsequence(vector<string>& words, vector<int>& groups) {
117+
int n = groups.size();
118118
vector<string> ans;
119119
for (int i = 0; i < n; ++i) {
120120
if (i == 0 || groups[i] != groups[i - 1]) {
@@ -129,7 +129,7 @@ public:
129129
#### Go
130130
131131
```go
132-
func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []string) {
132+
func getLongestSubsequence(words []string, groups []int) (ans []string) {
133133
for i, x := range groups {
134134
if i == 0 || x != groups[i-1] {
135135
ans = append(ans, words[i])
@@ -142,9 +142,9 @@ func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []st
142142
#### TypeScript
143143

144144
```ts
145-
function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] {
145+
function getLongestSubsequence(words: string[], groups: number[]): string[] {
146146
const ans: string[] = [];
147-
for (let i = 0; i < n; ++i) {
147+
for (let i = 0; i < groups.length; ++i) {
148148
if (i === 0 || groups[i] !== groups[i - 1]) {
149149
ans.push(words[i]);
150150
}
@@ -157,19 +157,13 @@ function getWordsInLongestSubsequence(n: number, words: string[], groups: number
157157

158158
```rust
159159
impl Solution {
160-
pub fn get_words_in_longest_subsequence(
161-
n: i32,
162-
words: Vec<String>,
163-
groups: Vec<i32>,
164-
) -> Vec<String> {
165-
let mut ans = vec![];
166-
167-
for i in 0..n {
168-
if i == 0 || groups[i as usize] != groups[(i - 1) as usize] {
169-
ans.push(words[i as usize].clone());
160+
pub fn get_longest_subsequence(words: Vec<String>, groups: Vec<i32>) -> Vec<String> {
161+
let mut ans = Vec::new();
162+
for (i, &g) in groups.iter().enumerate() {
163+
if i == 0 || g != groups[i - 1] {
164+
ans.push(words[i].clone());
170165
}
171166
}
172-
173167
ans
174168
}
175169
}

solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/README_EN.md

+13-19
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,16 @@ The time complexity is $O(n)$, where $n$ is the length of the array $groups$. Th
111111

112112
```python
113113
class Solution:
114-
def getWordsInLongestSubsequence(
115-
self, n: int, words: List[str], groups: List[int]
116-
) -> List[str]:
114+
def getLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]:
117115
return [words[i] for i, x in enumerate(groups) if i == 0 or x != groups[i - 1]]
118116
```
119117

120118
#### Java
121119

122120
```java
123121
class Solution {
124-
public List<String> getWordsInLongestSubsequence(int n, String[] words, int[] groups) {
122+
public List<String> getLongestSubsequence(String[] words, int[] groups) {
123+
int n = groups.length;
125124
List<String> ans = new ArrayList<>();
126125
for (int i = 0; i < n; ++i) {
127126
if (i == 0 || groups[i] != groups[i - 1]) {
@@ -138,7 +137,8 @@ class Solution {
138137
```cpp
139138
class Solution {
140139
public:
141-
vector<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
140+
vector<string> getLongestSubsequence(vector<string>& words, vector<int>& groups) {
141+
int n = groups.size();
142142
vector<string> ans;
143143
for (int i = 0; i < n; ++i) {
144144
if (i == 0 || groups[i] != groups[i - 1]) {
@@ -153,7 +153,7 @@ public:
153153
#### Go
154154
155155
```go
156-
func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []string) {
156+
func getLongestSubsequence(words []string, groups []int) (ans []string) {
157157
for i, x := range groups {
158158
if i == 0 || x != groups[i-1] {
159159
ans = append(ans, words[i])
@@ -166,9 +166,9 @@ func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []st
166166
#### TypeScript
167167

168168
```ts
169-
function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] {
169+
function getLongestSubsequence(words: string[], groups: number[]): string[] {
170170
const ans: string[] = [];
171-
for (let i = 0; i < n; ++i) {
171+
for (let i = 0; i < groups.length; ++i) {
172172
if (i === 0 || groups[i] !== groups[i - 1]) {
173173
ans.push(words[i]);
174174
}
@@ -181,19 +181,13 @@ function getWordsInLongestSubsequence(n: number, words: string[], groups: number
181181

182182
```rust
183183
impl Solution {
184-
pub fn get_words_in_longest_subsequence(
185-
n: i32,
186-
words: Vec<String>,
187-
groups: Vec<i32>,
188-
) -> Vec<String> {
189-
let mut ans = vec![];
190-
191-
for i in 0..n {
192-
if i == 0 || groups[i as usize] != groups[(i - 1) as usize] {
193-
ans.push(words[i as usize].clone());
184+
pub fn get_longest_subsequence(words: Vec<String>, groups: Vec<i32>) -> Vec<String> {
185+
let mut ans = Vec::new();
186+
for (i, &g) in groups.iter().enumerate() {
187+
if i == 0 || g != groups[i - 1] {
188+
ans.push(words[i].clone());
194189
}
195190
}
196-
197191
ans
198192
}
199193
}

solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class Solution {
22
public:
3-
vector<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
3+
vector<string> getLongestSubsequence(vector<string>& words, vector<int>& groups) {
4+
int n = groups.size();
45
vector<string> ans;
56
for (int i = 0; i < n; ++i) {
67
if (i == 0 || groups[i] != groups[i - 1]) {

solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []string) {
1+
func getLongestSubsequence(words []string, groups []int) (ans []string) {
22
for i, x := range groups {
33
if i == 0 || x != groups[i-1] {
44
ans = append(ans, words[i])

solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class Solution {
2-
public List<String> getWordsInLongestSubsequence(int n, String[] words, int[] groups) {
2+
public List<String> getLongestSubsequence(String[] words, int[] groups) {
3+
int n = groups.length;
34
List<String> ans = new ArrayList<>();
45
for (int i = 0; i < n; ++i) {
56
if (i == 0 || groups[i] != groups[i - 1]) {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
class Solution:
2-
def getWordsInLongestSubsequence(
3-
self, n: int, words: List[str], groups: List[int]
4-
) -> List[str]:
2+
def getLongestSubsequence(self, words: List[str], groups: List[int]) -> List[str]:
53
return [words[i] for i, x in enumerate(groups) if i == 0 or x != groups[i - 1]]
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
impl Solution {
2-
pub fn get_words_in_longest_subsequence(
3-
n: i32,
4-
words: Vec<String>,
5-
groups: Vec<i32>,
6-
) -> Vec<String> {
7-
let mut ans = vec![];
8-
9-
for i in 0..n {
10-
if i == 0 || groups[i as usize] != groups[(i - 1) as usize] {
11-
ans.push(words[i as usize].clone());
2+
pub fn get_longest_subsequence(words: Vec<String>, groups: Vec<i32>) -> Vec<String> {
3+
let mut ans = Vec::new();
4+
for (i, &g) in groups.iter().enumerate() {
5+
if i == 0 || g != groups[i - 1] {
6+
ans.push(words[i].clone());
127
}
138
}
14-
159
ans
1610
}
1711
}

solution/2900-2999/2900.Longest Unequal Adjacent Groups Subsequence I/Solution.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] {
1+
function getLongestSubsequence(words: string[], groups: number[]): string[] {
22
const ans: string[] = [];
3-
for (let i = 0; i < n; ++i) {
3+
for (let i = 0; i < groups.length; ++i) {
44
if (i === 0 || groups[i] !== groups[i - 1]) {
55
ans.push(words[i]);
66
}

solution/2900-2999/2901.Longest Unequal Adjacent Groups Subsequence II/README.md

+13-76
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,12 @@ tags:
103103
```python
104104
class Solution:
105105
def getWordsInLongestSubsequence(
106-
self, n: int, words: List[str], groups: List[int]
106+
self, words: List[str], groups: List[int]
107107
) -> List[str]:
108108
def check(s: str, t: str) -> bool:
109109
return len(s) == len(t) and sum(a != b for a, b in zip(s, t)) == 1
110110

111+
n = len(groups)
111112
f = [1] * n
112113
g = [-1] * n
113114
mx = 1
@@ -132,7 +133,8 @@ class Solution:
132133

133134
```java
134135
class Solution {
135-
public List<String> getWordsInLongestSubsequence(int n, String[] words, int[] groups) {
136+
public List<String> getWordsInLongestSubsequence(String[] words, int[] groups) {
137+
int n = groups.length;
136138
int[] f = new int[n];
137139
int[] g = new int[n];
138140
Arrays.fill(f, 1);
@@ -180,7 +182,7 @@ class Solution {
180182
```cpp
181183
class Solution {
182184
public:
183-
vector<string> getWordsInLongestSubsequence(int n, vector<string>& words, vector<int>& groups) {
185+
vector<string> getWordsInLongestSubsequence(vector<string>& words, vector<int>& groups) {
184186
auto check = [](string& s, string& t) {
185187
if (s.size() != t.size()) {
186188
return false;
@@ -191,6 +193,7 @@ public:
191193
}
192194
return cnt == 1;
193195
};
196+
int n = groups.size();
194197
vector<int> f(n, 1);
195198
vector<int> g(n, -1);
196199
int mx = 1;
@@ -221,7 +224,7 @@ public:
221224
#### Go
222225
223226
```go
224-
func getWordsInLongestSubsequence(n int, words []string, groups []int) []string {
227+
func getWordsInLongestSubsequence(words []string, groups []int) []string {
225228
check := func(s, t string) bool {
226229
if len(s) != len(t) {
227230
return false
@@ -234,6 +237,7 @@ func getWordsInLongestSubsequence(n int, words []string, groups []int) []string
234237
}
235238
return cnt == 1
236239
}
240+
n := len(groups)
237241
f := make([]int, n)
238242
g := make([]int, n)
239243
for i := range f {
@@ -261,17 +265,16 @@ func getWordsInLongestSubsequence(n int, words []string, groups []int) []string
261265
break
262266
}
263267
}
264-
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
265-
ans[i], ans[j] = ans[j], ans[i]
266-
}
268+
slices.Reverse(ans)
267269
return ans
268270
}
269271
```
270272

271273
#### TypeScript
272274

273275
```ts
274-
function getWordsInLongestSubsequence(n: number, words: string[], groups: number[]): string[] {
276+
function getWordsInLongestSubsequence(words: string[], groups: number[]): string[] {
277+
const n = groups.length;
275278
const f: number[] = Array(n).fill(1);
276279
const g: number[] = Array(n).fill(-1);
277280
let mx = 1;
@@ -313,16 +316,12 @@ function getWordsInLongestSubsequence(n: number, words: string[], groups: number
313316

314317
```rust
315318
impl Solution {
316-
pub fn get_words_in_longest_subsequence(
317-
n: i32,
318-
words: Vec<String>,
319-
groups: Vec<i32>,
320-
) -> Vec<String> {
319+
pub fn get_words_in_longest_subsequence(words: Vec<String>, groups: Vec<i32>) -> Vec<String> {
321320
fn check(s: &str, t: &str) -> bool {
322321
s.len() == t.len() && s.chars().zip(t.chars()).filter(|(a, b)| a != b).count() == 1
323322
}
324323

325-
let n = n as usize;
324+
let n = groups.len();
326325

327326
let mut f = vec![1; n];
328327
let mut g = vec![-1; n];
@@ -364,66 +363,4 @@ impl Solution {
364363

365364
<!-- solution:end -->
366365

367-
<!-- solution:start -->
368-
369-
### 方法二:动态规划 + 通配符哈希表
370-
371-
在方法一中,我们需要枚举所有的 $i$ 和 $j$ 组合, 这一步可以通过维护一个通配符哈希表来优化. 对于每个字符串 $word[i]$, 我们枚举它的每个字符, 将其替换为通配符, 然后将替换后的字符串作为键, 将其下标作为值存入哈希表中. 这样我们可以在 $O(L)$ 时间内找到所有距离 $word[i]$ 汉明距离为 1 的 $word[j]$. 尽管时间复杂度仍然是 $O(n^2 \times L)$, 但平均复杂度会有所降低.
372-
373-
<!-- tabs:start -->
374-
375-
#### Java
376-
377-
```java
378-
class Solution {
379-
public List<String> getWordsInLongestSubsequence(int n, String[] words, int[] groups) {
380-
int[] dp = new int[n];
381-
int[] next = new int[n];
382-
Map<String, List<Integer>> strToIdxMap = new HashMap<>();
383-
int maxIdx = n;
384-
for (int i = n - 1; i >= 0; i--) {
385-
int prevIdx = n;
386-
char[] word = words[i].toCharArray();
387-
for (int j = 0; j < word.length; j++) {
388-
// convert word to pattern with '*'.
389-
char temp = word[j];
390-
word[j] = '*';
391-
String curr = new String(word);
392-
393-
// search matches and update dp.
394-
List<Integer> prevList = strToIdxMap.getOrDefault(curr, List.of());
395-
for (int prev : prevList) {
396-
if (groups[prev] == groups[i] || dp[prev] < dp[i]) {
397-
continue;
398-
}
399-
dp[i] = dp[prev] + 1;
400-
prevIdx = prev;
401-
}
402-
403-
// append current pattern to dictionary.
404-
strToIdxMap.computeIfAbsent(curr, k -> new ArrayList<>()).add(i);
405-
406-
// restore pattern to orignal word.
407-
word[j] = temp;
408-
}
409-
if (maxIdx >= n || dp[i] > dp[maxIdx]) {
410-
maxIdx = i;
411-
}
412-
next[i] = prevIdx;
413-
}
414-
int curr = maxIdx;
415-
List<String> ans = new ArrayList<>();
416-
while (curr < n) {
417-
ans.add(words[curr]);
418-
curr = next[curr];
419-
}
420-
return ans;
421-
}
422-
}
423-
```
424-
425-
<!-- tabs:end -->
426-
427-
<!-- solution:end -->
428-
429366
<!-- problem:end -->

0 commit comments

Comments
 (0)