File tree 6 files changed +218
-0
lines changed
solution/3100-3199/3163.String Compression III
6 files changed +218
-0
lines changed Original file line number Diff line number Diff line change @@ -213,4 +213,90 @@ function compressedString(word: string): string {
213
213
214
214
<!-- solution: end -->
215
215
216
+ <!-- solution: start -->
217
+
218
+ ### 方法二:双指针
219
+
220
+ <!-- tabs: start -->
221
+
222
+ #### TypeScript
223
+
224
+ ``` ts
225
+ function compressedString(word : string ): string {
226
+ let res = ' ' ;
227
+
228
+ for (let i = 1 , j = 0 ; i <= word .length ; i ++ ) {
229
+ if (word [i ] !== word [j ] || i - j === 9 ) {
230
+ res += i - j + word [j ];
231
+ j = i ;
232
+ }
233
+ }
234
+
235
+ return res ;
236
+ }
237
+ ```
238
+
239
+ #### JavaScript
240
+
241
+ ``` js
242
+ function compressedString (word ) {
243
+ let res = ' ' ;
244
+
245
+ for (let i = 1 , j = 0 ; i <= word .length ; i++ ) {
246
+ if (word[i] !== word[j] || i - j === 9 ) {
247
+ res += i - j + word[j];
248
+ j = i;
249
+ }
250
+ }
251
+
252
+ return res;
253
+ }
254
+ ```
255
+
256
+ <!-- tabs: end -->
257
+
258
+ <!-- solution: end -->
259
+
260
+ <!-- solution: start -->
261
+
262
+ ### 方法三:正则匹配
263
+
264
+ <!-- tabs: start -->
265
+
266
+ #### TypeScript
267
+
268
+ ``` ts
269
+ function compressedString(word : string ): string {
270
+ const regex = / (. )\1 {0,8} / g ;
271
+ let m: RegExpMatchArray | null = null ;
272
+ let res = ' ' ;
273
+
274
+ while ((m = regex .exec (word ))) {
275
+ res += m [0 ].length + m [1 ];
276
+ }
277
+
278
+ return res ;
279
+ }
280
+ ```
281
+
282
+ #### JavaScript
283
+
284
+ ``` js
285
+ function compressedString (word ) {
286
+ const regex = / (. )\1 {0,8} / g ;
287
+ let m = null ;
288
+ let res = ' ' ;
289
+
290
+ while ((m = regex .exec (word))) {
291
+ res += m[0 ].length + m[1 ];
292
+ }
293
+
294
+ return res;
295
+ }
296
+ ```
297
+
298
+ <!-- tabs: end -->
299
+
300
+ <!-- solution: end -->
301
+
216
302
<!-- problem: end -->
Original file line number Diff line number Diff line change @@ -209,4 +209,90 @@ function compressedString(word: string): string {
209
209
210
210
<!-- solution: end -->
211
211
212
+ <!-- solution: start -->
213
+
214
+ ### Solution 2: Two Pointers
215
+
216
+ <!-- tabs: start -->
217
+
218
+ #### TypeScript
219
+
220
+ ``` ts
221
+ function compressedString(word : string ): string {
222
+ let res = ' ' ;
223
+
224
+ for (let i = 1 , j = 0 ; i <= word .length ; i ++ ) {
225
+ if (word [i ] !== word [j ] || i - j === 9 ) {
226
+ res += i - j + word [j ];
227
+ j = i ;
228
+ }
229
+ }
230
+
231
+ return res ;
232
+ }
233
+ ```
234
+
235
+ #### JavaScript
236
+
237
+ ``` js
238
+ function compressedString (word ) {
239
+ let res = ' ' ;
240
+
241
+ for (let i = 1 , j = 0 ; i <= word .length ; i++ ) {
242
+ if (word[i] !== word[j] || i - j === 9 ) {
243
+ res += i - j + word[j];
244
+ j = i;
245
+ }
246
+ }
247
+
248
+ return res;
249
+ }
250
+ ```
251
+
252
+ <!-- tabs: end -->
253
+
254
+ <!-- solution: end -->
255
+
256
+ <!-- solution: start -->
257
+
258
+ ### Solution 3: RegExp
259
+
260
+ <!-- tabs: start -->
261
+
262
+ #### TypeScript
263
+
264
+ ``` ts
265
+ function compressedString(word : string ): string {
266
+ const regex = / (. )\1 {0,8} / g ;
267
+ let m: RegExpMatchArray | null = null ;
268
+ let res = ' ' ;
269
+
270
+ while ((m = regex .exec (word ))) {
271
+ res += m [0 ].length + m [1 ];
272
+ }
273
+
274
+ return res ;
275
+ }
276
+ ```
277
+
278
+ #### JavaScript
279
+
280
+ ``` js
281
+ function compressedString (word ) {
282
+ const regex = / (. )\1 {0,8} / g ;
283
+ let m = null ;
284
+ let res = ' ' ;
285
+
286
+ while ((m = regex .exec (word))) {
287
+ res += m[0 ].length + m[1 ];
288
+ }
289
+
290
+ return res;
291
+ }
292
+ ```
293
+
294
+ <!-- tabs: end -->
295
+
296
+ <!-- solution: end -->
297
+
212
298
<!-- problem: end -->
Original file line number Diff line number Diff line change
1
+ function compressedString ( word ) {
2
+ let res = '' ;
3
+
4
+ for ( let i = 1 , j = 0 ; i <= word . length ; i ++ ) {
5
+ if ( word [ i ] !== word [ j ] || i - j === 9 ) {
6
+ res += i - j + word [ j ] ;
7
+ j = i ;
8
+ }
9
+ }
10
+
11
+ return res ;
12
+ }
Original file line number Diff line number Diff line change
1
+ function compressedString ( word : string ) : string {
2
+ let res = '' ;
3
+
4
+ for ( let i = 1 , j = 0 ; i <= word . length ; i ++ ) {
5
+ if ( word [ i ] !== word [ j ] || i - j === 9 ) {
6
+ res += i - j + word [ j ] ;
7
+ j = i ;
8
+ }
9
+ }
10
+
11
+ return res ;
12
+ }
Original file line number Diff line number Diff line change
1
+ function compressedString ( word ) {
2
+ const regex = / ( .) \1{ 0 , 8 } / g;
3
+ let m = null ;
4
+ let res = '' ;
5
+
6
+ while ( ( m = regex . exec ( word ) ) ) {
7
+ res += m [ 0 ] . length + m [ 1 ] ;
8
+ }
9
+
10
+ return res ;
11
+ }
Original file line number Diff line number Diff line change
1
+ function compressedString ( word : string ) : string {
2
+ const regex = / ( .) \1{ 0 , 8 } / g;
3
+ let m : RegExpMatchArray | null = null ;
4
+ let res = '' ;
5
+
6
+ while ( ( m = regex . exec ( word ) ) ) {
7
+ res += m [ 0 ] . length + m [ 1 ] ;
8
+ }
9
+
10
+ return res ;
11
+ }
You can’t perform that action at this time.
0 commit comments