File tree Expand file tree Collapse file tree 1 file changed +71
-0
lines changed Expand file tree Collapse file tree 1 file changed +71
-0
lines changed Original file line number Diff line number Diff line change @@ -289,8 +289,79 @@ func repeatedSubstringPattern(s string) bool {
289
289
}
290
290
```
291
291
292
+ JavaScript版本
292
293
294
+ > 前缀表统一减一
293
295
296
+ ```javascript
297
+ /* *
298
+ * @param {string} s
299
+ * @return {boolean}
300
+ */
301
+ var repeatedSubstringPattern = function (s) {
302
+ if (s.length === 0)
303
+ return false;
304
+
305
+ const getNext = (s) => {
306
+ let next = [];
307
+ let j = -1;
308
+
309
+ next.push(j);
310
+
311
+ for (let i = 1; i < s.length; ++i) {
312
+ while (j >= 0 && s[i] !== s[j + 1])
313
+ j = next[j];
314
+ if (s[i] === s[j + 1])
315
+ j++;
316
+ next.push(j);
317
+ }
318
+
319
+ return next;
320
+ }
321
+
322
+ let next = getNext(s);
323
+
324
+ if (next[next.length - 1] !== -1 && s.length % (s.length - (next[next.length - 1] + 1)) === 0)
325
+ return true;
326
+ return false;
327
+ };
328
+ ```
329
+
330
+ > 前缀表统一不减一
331
+
332
+ ``` javascript
333
+ /**
334
+ * @param {string} s
335
+ * @return {boolean}
336
+ */
337
+ var repeatedSubstringPattern = function (s ) {
338
+ if (s .length === 0 )
339
+ return false ;
340
+
341
+ const getNext = (s ) => {
342
+ let next = [];
343
+ let j = 0 ;
344
+
345
+ next .push (j);
346
+
347
+ for (let i = 1 ; i < s .length ; ++ i) {
348
+ while (j > 0 && s[i] !== s[j])
349
+ j = next[j - 1 ];
350
+ if (s[i] === s[j])
351
+ j++ ;
352
+ next .push (j);
353
+ }
354
+
355
+ return next;
356
+ }
357
+
358
+ let next = getNext (s);
359
+
360
+ if (next[next .length - 1 ] !== 0 && s .length % (s .length - next[next .length - 1 ]) === 0 )
361
+ return true ;
362
+ return false ;
363
+ };
364
+ ```
294
365
295
366
296
367
You can’t perform that action at this time.
0 commit comments