大佬教程收集整理的这篇文章主要介绍了[Swift]LeetCode459. 重复的子字符串 | Repeated Substring Pattern,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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.、
Example 1:
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。
示例 1:
输入: "abab" 输出: True 解释: 可由子字符串 "ab" 重复两次构成。
示例 2:
输入: "aba" 输出: false
示例 3:
输入: "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 }
292ms
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 }
以上是大佬教程为你收集整理的[Swift]LeetCode459. 重复的子字符串 | Repeated Substring Pattern全部内容,希望文章能够帮你解决[Swift]LeetCode459. 重复的子字符串 | Repeated Substring Pattern所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。