为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode459. 重复的子字符串 | Repeated Substring Pattern

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10343514.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 }

 

posted @ 2019-01-31 19:24  为敢技术  阅读(348)  评论(0编辑  收藏  举报