From 3dcaa43ffe0e6b585e0eb2c372ea7786c1e7cca6 Mon Sep 17 00:00:00 2001 From: kok-s0s <2694308562@qq.com> Date: Wed, 28 Jul 2021 23:48:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BE=9BJavaScript=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E3=80=8A=E9=87=8D=E5=A4=8D=E7=9A=84=E5=AD=90=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E3=80=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0459.重复的子字符串.md | 71 ++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/problems/0459.重复的子字符串.md b/problems/0459.重复的子字符串.md index f012811d..25b52e86 100644 --- a/problems/0459.重复的子字符串.md +++ b/problems/0459.重复的子字符串.md @@ -289,8 +289,79 @@ func repeatedSubstringPattern(s string) bool { } ``` +JavaScript版本 +> 前缀表统一减一 +```javascript +/** + * @param {string} s + * @return {boolean} + */ +var repeatedSubstringPattern = function (s) { + if (s.length === 0) + return false; + + const getNext = (s) => { + let next = []; + let j = -1; + + next.push(j); + + for (let i = 1; i < s.length; ++i) { + while (j >= 0 && s[i] !== s[j + 1]) + j = next[j]; + if (s[i] === s[j + 1]) + j++; + next.push(j); + } + + return next; + } + + let next = getNext(s); + + if (next[next.length - 1] !== -1 && s.length % (s.length - (next[next.length - 1] + 1)) === 0) + return true; + return false; +}; +``` + +> 前缀表统一不减一 + +```javascript +/** + * @param {string} s + * @return {boolean} + */ +var repeatedSubstringPattern = function (s) { + if (s.length === 0) + return false; + + const getNext = (s) => { + let next = []; + let j = 0; + + next.push(j); + + for (let i = 1; i < s.length; ++i) { + while (j > 0 && s[i] !== s[j]) + j = next[j - 1]; + if (s[i] === s[j]) + j++; + next.push(j); + } + + return next; + } + + let next = getNext(s); + + if (next[next.length - 1] !== 0 && s.length % (s.length - next[next.length - 1]) === 0) + return true; + return false; +}; +```