@@ -90,32 +90,212 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3606.Co
90
90
91
91
<!-- solution:start -->
92
92
93
- ### 方法一
93
+ ### 方法一:模拟
94
+
95
+ 我们可以直接模拟题目中的条件来筛选出有效的优惠券。具体步骤如下:
96
+
97
+ 1 . ** 检查标识符** :对于每个优惠券的标识符,检查它是否非空,并且只包含字母、数字和下划线。
98
+ 2 . ** 检查业务类别** :检查每个优惠券的业务类别是否属于给定的四个有效类别之一。
99
+ 3 . ** 检查激活状态** :检查每个优惠券是否处于激活状态。
100
+ 4 . ** 收集有效优惠券** :将所有满足上述条件的优惠券的 id 收集起来。
101
+ 5 . ** 排序** :根据业务类别和标识符对有效优惠券进行排序。
102
+ 6 . ** 返回结果** :返回排序后的有效优惠券的标识符列表。
103
+
104
+ 时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$,其中 $n$ 是优惠券的数量。
94
105
95
106
<!-- tabs:start -->
96
107
97
108
#### Python3
98
109
99
110
``` python
100
-
111
+ class Solution :
112
+ def validateCoupons (
113
+ self , code : List[str ], businessLine : List[str ], isActive : List[bool ]
114
+ ) -> List[str ]:
115
+ def check (s : str ) -> bool :
116
+ if not s:
117
+ return False
118
+ for c in s:
119
+ if not (c.isalpha() or c.isdigit() or c == " _" ):
120
+ return False
121
+ return True
122
+
123
+ idx = []
124
+ bs = {" electronics" , " grocery" , " pharmacy" , " restaurant" }
125
+ for i, (c, b, a) in enumerate (zip (code, businessLine, isActive)):
126
+ if a and b in bs and check(c):
127
+ idx.append(i)
128
+ idx.sort(key = lambda i : (businessLine[i], code[i]))
129
+ return [code[i] for i in idx]
101
130
```
102
131
103
132
#### Java
104
133
105
134
``` java
106
-
135
+ class Solution {
136
+ public List<String > validateCoupons (String [] code , String [] businessLine , boolean [] isActive ) {
137
+ List<Integer > idx = new ArrayList<> ();
138
+ Set<String > bs
139
+ = new HashSet<> (Arrays . asList(" electronics" , " grocery" , " pharmacy" , " restaurant" ));
140
+
141
+ for (int i = 0 ; i < code. length; i++ ) {
142
+ if (isActive[i] && bs. contains(businessLine[i]) && check(code[i])) {
143
+ idx. add(i);
144
+ }
145
+ }
146
+
147
+ idx. sort((i, j) - > {
148
+ int cmp = businessLine[i]. compareTo(businessLine[j]);
149
+ if (cmp != 0 ) {
150
+ return cmp;
151
+ }
152
+ return code[i]. compareTo(code[j]);
153
+ });
154
+
155
+ List<String > ans = new ArrayList<> ();
156
+ for (int i : idx) {
157
+ ans. add(code[i]);
158
+ }
159
+ return ans;
160
+ }
161
+
162
+ private boolean check (String s ) {
163
+ if (s. isEmpty()) {
164
+ return false ;
165
+ }
166
+ for (char c : s. toCharArray()) {
167
+ if (! Character . isLetterOrDigit(c) && c != ' _' ) {
168
+ return false ;
169
+ }
170
+ }
171
+ return true ;
172
+ }
173
+ }
107
174
```
108
175
109
176
#### C++
110
177
111
178
``` cpp
112
-
179
+ class Solution {
180
+ public:
181
+ vector<string > validateCoupons(vector<string >& code, vector<string >& businessLine, vector<bool >& isActive) {
182
+ vector<int > idx;
183
+ unordered_set<string > bs = {"electronics", "grocery", "pharmacy", "restaurant"};
184
+
185
+ for (int i = 0; i < code.size(); ++i) {
186
+ const string& c = code[i];
187
+ const string& b = businessLine[i];
188
+ bool a = isActive[i];
189
+ if (a && bs.count(b) && check(c)) {
190
+ idx.push_back(i);
191
+ }
192
+ }
193
+
194
+ sort (idx.begin(), idx.end(), [ &] (int i, int j) {
195
+ if (businessLine[ i] != businessLine[ j] ) return businessLine[ i] < businessLine[ j] ;
196
+ return code[ i] < code[ j] ;
197
+ });
198
+
199
+ vector<string> ans;
200
+ for (int i : idx) {
201
+ ans.push_back(code[i]);
202
+ }
203
+ return ans;
204
+ }
205
+
206
+ private:
207
+ bool check(const string& s) {
208
+ if (s.empty()) return false;
209
+ for (char c : s) {
210
+ if (!isalnum(c) && c != '_ ') {
211
+ return false;
212
+ }
213
+ }
214
+ return true;
215
+ }
216
+ };
113
217
```
114
218
115
219
#### Go
116
220
117
221
```go
222
+ func validateCoupons(code []string, businessLine []string, isActive []bool) []string {
223
+ idx := []int{}
224
+ bs := map[string]struct{}{
225
+ "electronics": {},
226
+ "grocery": {},
227
+ "pharmacy": {},
228
+ "restaurant": {},
229
+ }
230
+
231
+ check := func(s string) bool {
232
+ if len(s) == 0 {
233
+ return false
234
+ }
235
+ for _, c := range s {
236
+ if !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_' {
237
+ return false
238
+ }
239
+ }
240
+ return true
241
+ }
242
+
243
+ for i := range code {
244
+ if isActive[i] {
245
+ if _, ok := bs[businessLine[i]]; ok && check(code[i]) {
246
+ idx = append(idx, i)
247
+ }
248
+ }
249
+ }
250
+
251
+ sort.Slice(idx, func(i, j int) bool {
252
+ if businessLine[idx[i]] != businessLine[idx[j]] {
253
+ return businessLine[idx[i]] < businessLine[idx[j]]
254
+ }
255
+ return code[idx[i]] < code[idx[j]]
256
+ })
257
+
258
+ ans := make([]string, 0, len(idx))
259
+ for _, i := range idx {
260
+ ans = append(ans, code[i])
261
+ }
262
+ return ans
263
+ }
264
+ ```
118
265
266
+ #### TypeScript
267
+
268
+ ``` ts
269
+ function validateCoupons(code : string [], businessLine : string [], isActive : boolean []): string [] {
270
+ const idx: number [] = [];
271
+ const bs = new Set ([' electronics' , ' grocery' , ' pharmacy' , ' restaurant' ]);
272
+
273
+ const check = (s : string ): boolean => {
274
+ if (s .length === 0 ) return false ;
275
+ for (let i = 0 ; i < s .length ; i ++ ) {
276
+ const c = s [i ];
277
+ if (! / [a-zA-Z0-9 _] / .test (c )) {
278
+ return false ;
279
+ }
280
+ }
281
+ return true ;
282
+ };
283
+
284
+ for (let i = 0 ; i < code .length ; i ++ ) {
285
+ if (isActive [i ] && bs .has (businessLine [i ]) && check (code [i ])) {
286
+ idx .push (i );
287
+ }
288
+ }
289
+
290
+ idx .sort ((i , j ) => {
291
+ if (businessLine [i ] !== businessLine [j ]) {
292
+ return businessLine [i ] < businessLine [j ] ? - 1 : 1 ;
293
+ }
294
+ return code [i ] < code [j ] ? - 1 : 1 ;
295
+ });
296
+
297
+ return idx .map (i => code [i ]);
298
+ }
119
299
```
120
300
121
301
<!-- tabs:end -->
0 commit comments