459.Repeated Substring Pattern === ###### tags: `Easy` `String` [459. Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/) ### 題目描述 Given a string `s`, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. ### 範例 **Example 1:** ``` Input: s = "abab" Output: true Explanation: It is the substring "ab" twice. ``` **Example 2:** ``` Input: s = "aba" Output: false ``` **Example 3:** ``` Input: s = "abcabcabcabc" Output: true Explanation: It is the substring "abc" four times or the substring "abcabc" twice. ``` **Constraints**: * 1 <= `s.length` <= 10^4^ * `s` consists of lowercase English letters. ### 解答 #### Javascript ```javascript= function repeatedSubstringPattern(s) { const len = s.length >> 1; let subString = ''; for (let i = 0; i < len; i++) { subString += s[i]; let tempString = subString; while (tempString.length < s.length) { tempString += subString; } if (tempString === s) return true; } return false; } ``` ```javascript= function repeatedSubstringPattern2(s) { return s.repeat(2).slice(1, -1).includes(s); } ``` > 下面一行的解是我弟教我的,跟鬼一樣。 > [name=Marsgoat][time=Aug 21, 2023] ### Reference [回到題目列表](https://marsgoat.github.io/XNnote/coding/leetcodeEveryDay.html)