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: TrueExplanation: It‘s the substring "ab" twice.
Example 2:
input: "aba"Output: False
Example 3:
input: "abcabcabcabc"Output: TrueExplanation: 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 = 110 var j:Int = 011 var n:Int = s.count12 var dp:[Int] = [Int](repeating:0,count:n + 1)13 while(i < n)14 {15 if arr[i] == arr[j]16 {17 i += 118 j += 119 dp[i] = j20 }21 else if j == 022 {23 i += 124 }25 else26 {27 j = dp[j]28 }29 }30 return dp[n] % (n - dp[n]) == 0 && dp[n] != 031 }32 }
292ms
1 class Solution {2 func repeatedSubstringPattern(_ s: String) -> Bool {3 let ss = s + s4 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 true19 }20 21 }22 index -= 123 }24 25 return false26 }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 / huff11 var sumString = ""12 13 while num > 0 {14 sumString = sumString + subString15 num = num - 116 }17 18 if sumString == s {19 return true20 }21 }22 23 huff = huff - 124 }25 return false26 }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到index10 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 result36 }37 }总结
以上是内存溢出为你收集整理的[Swift]LeetCode459. 重复的子字符串 | Repeated Substring Pattern全部内容,希望文章能够帮你解决[Swift]LeetCode459. 重复的子字符串 | Repeated Substring Pattern所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)