@@ -111,17 +111,16 @@ The time complexity is $O(n)$, where $n$ is the length of the array $groups$. Th
111
111
112
112
``` python
113
113
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 ]:
117
115
return [words[i] for i, x in enumerate (groups) if i == 0 or x != groups[i - 1 ]]
118
116
```
119
117
120
118
#### Java
121
119
122
120
``` java
123
121
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;
125
124
List<String > ans = new ArrayList<> ();
126
125
for (int i = 0 ; i < n; ++ i) {
127
126
if (i == 0 || groups[i] != groups[i - 1 ]) {
@@ -138,7 +137,8 @@ class Solution {
138
137
``` cpp
139
138
class Solution {
140
139
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();
142
142
vector<string > ans;
143
143
for (int i = 0; i < n; ++i) {
144
144
if (i == 0 || groups[ i] != groups[ i - 1] ) {
@@ -153,22 +153,22 @@ public:
153
153
#### Go
154
154
155
155
```go
156
- func getWordsInLongestSubsequence(n int, words []string, groups []int) (ans []string) {
157
- for i, x := range groups {
158
- if i == 0 || x != groups[i- 1] {
159
- ans = append(ans, words[i])
160
- }
161
- }
162
- return
156
+ func getLongestSubsequence( words []string, groups []int) (ans []string) {
157
+ for i, x := range groups {
158
+ if i == 0 || x != groups[i - 1] {
159
+ ans = append(ans, words[i])
160
+ }
161
+ }
162
+ return
163
163
}
164
164
```
165
165
166
166
#### TypeScript
167
167
168
168
``` ts
169
- function getWordsInLongestSubsequence( n : number , words : string [], groups : number []): string [] {
169
+ function getLongestSubsequence( words : string [], groups : number []): string [] {
170
170
const ans: string [] = [];
171
- for (let i = 0 ; i < n ; ++ i ) {
171
+ for (let i = 0 ; i < groups . length ; ++ i ) {
172
172
if (i === 0 || groups [i ] !== groups [i - 1 ]) {
173
173
ans .push (words [i ]);
174
174
}
@@ -181,19 +181,13 @@ function getWordsInLongestSubsequence(n: number, words: string[], groups: number
181
181
182
182
``` rust
183
183
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 ());
194
189
}
195
190
}
196
-
197
191
ans
198
192
}
199
193
}
0 commit comments