★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-fbjxusnr-hy.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.、 git
Example 1:github
Input: "abab" Output: True Explanation: It's the substring "ab" twice.
Example 2:算法
Input: "aba" Output: False
Example 3:微信
Input: "abcabcabcabc" Output: True Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)
給定一個非空的字符串,判斷它是否能夠由它的一個子串重複屢次構成。給定的字符串只含有小寫英文字母,而且長度不超過10000。app
示例 1:spa
輸入: "abab" 輸出: True 解釋: 可由子字符串 "ab" 重複兩次構成。
示例 2:code
輸入: "aba" 輸出: False
示例 3:htm
輸入: "abcabcabcabc" 輸出: True 解釋: 可由子字符串 "abc" 重複四次構成。 (或者子字符串 "abcabc" 重複兩次構成。)
148ms
1 class Solution { 2 //kmp算法 3 func repeatedSubstringPattern(_ s: String) -> Bool { 4 var arr:[Character] = [Character]() 5 for char in s.characters 6 { 7 arr.append(char) 8 } 9 var i:Int = 1 10 var j:Int = 0 11 var n:Int = s.count 12 var dp:[Int] = [Int](repeating:0,count:n + 1) 13 while(i < n) 14 { 15 if arr[i] == arr[j] 16 { 17 i += 1 18 j += 1 19 dp[i] = j 20 } 21 else if j == 0 22 { 23 i += 1 24 } 25 else 26 { 27 j = dp[j] 28 } 29 } 30 return dp[n] % (n - dp[n]) == 0 && dp[n] != 0 31 } 32 }
292msblog
1 class Solution { 2 func repeatedSubstringPattern(_ s: String) -> Bool { 3 let ss = s + s 4 let str = ss[ss.index(after: ss.startIndex)..<ss.index(before: ss.endIndex)] 5 return str.contains(s) 6 } 7 }
480ms
1 class Solution { 2 func repeatedSubstringPattern(_ s: String) -> Bool { 3 let length = s.count 4 var index = length / 2 5 6 while index >= 1 { 7 if length % index == 0 { 8 let c = length / index 9 var current = "" 10 11 for _ in 0..<c { 12 13 let offset = s.index(s.startIndex, offsetBy: index) 14 current += String(s[..<offset]) 15 16 } 17 if current == s { 18 return true 19 } 20 21 } 22 index -= 1 23 } 24 25 return false 26 } 27 }
500ms
1 class Solution { 2 func repeatedSubstringPattern(_ s: String) -> Bool { 3 let chas = [Character](s) 4 let res = String(chas[1...]) + String(chas[..<(chas.count-1)]) 5 6 return res.contains(s) 7 } 8 }
604ms
1 class Solution { 2 func repeatedSubstringPattern(_ s: String) -> Bool { 3 let count = s.count 4 var huff = count / 2 5 while huff >= 1 { 6 if count % huff == 0 { 7 let toIndex = s.index(s.startIndex, offsetBy: huff) 8 let subString = s[s.startIndex..<toIndex] 9 10 var num = count / huff 11 var sumString = "" 12 13 while num > 0 { 14 sumString = sumString + subString 15 num = num - 1 16 } 17 18 if sumString == s { 19 return true 20 } 21 } 22 23 huff = huff - 1 24 } 25 return false 26 } 27 }
3292ms
1 class Solution { 2 func repeatedSubstringPattern(_ s: String) -> Bool { 3 let length = s.count 4 5 var result = false; 6 for index in 1...length { 7 // 整除則對比 8 if length % (index) == 0 { 9 // 從0到index 10 let character = s.prefix(index) 11 let increment = index; 12 var start = increment; 13 14 var isEqual = false; 15 while (start < length) { 16 let begin = s.index(s.startIndex, offsetBy: start) 17 let stop = s.index(s.startIndex, offsetBy: start + increment) 18 let temp = s[begin..<stop] 19 20 if (character == temp) { 21 start += increment; 22 isEqual = true; 23 continue; 24 } else { 25 isEqual = false; 26 break; 27 } 28 } 29 result = isEqual; 30 if isEqual { 31 break; 32 } 33 } 34 } 35 return result 36 } 37 }