[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 }

原文地址:https://www.cnblogs.com/strengthen/p/10343514.html

时间: 2024-07-31 10:54:58

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

LeetCode 459. 重复的子字符串(Repeated Substring Pattern)

459. 重复的子字符串 459. Repeated Substring Pattern 题目描述 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过 10000. LeetCode459. Repeated Substring Pattern 示例 1: 输入: "abab" 输出: True 解释: 可由子字符串 "ab" 重复两次构成. 示例 2: 输入: "aba" 输出: Fa

Leetcode 459.重复的子字符串

重复的子字符串 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" 输出: True 解释: 可由子字符串 "ab" 重复两次构成. 示例 2: 输入: "aba" 输出: False 示例 3: 输入: "abcabcabcabc" 输出: True 解释: 可由子字符串 "abc" 重复四次构成.

459. Repeated Substring Pattern【easy】

459. Repeated Substring Pattern[easy] 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 o

43. leetcode 459. Repeated Substring Pattern

459. 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 an

【leetcode 简单】 第一百一十二题 重复的子字符串

给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且长度不超过10000. 示例 1: 输入: "abab" 输出: True 解释: 可由子字符串 "ab" 重复两次构成. 示例 2: 输入: "aba" 输出: False 示例 3: 输入: "abcabcabcabc" 输出: True 解释: 可由子字符串 "abc" 重复四次构成. (或者子字符串

【LeetCode】459. 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 100

459.(KMP)求字符串是否由模式重复构成 Repeated Substring Pattern

假设str长度为len,重复的子串长度为k,则如果真的由连续多个长度为k的子串重复构成str,那么在对str求next时,由于连续对称性(如图,前后两个虚线框内字符串相等),会从next[k+1]开始,1,2,3...地递增,直到next[len]=len-k,且(len-k)%k==0,表示有整数个k 要一直求到next[len]而不是next[len-1],是因为next[len-1]只是表示前len-1个字母的内部对称性,而没有考虑到最后一个字母即str[len-1] 所以求解很简单:先对

[Swift]LeetCode187. 重复的DNA序列 | Repeated DNA Sequences

All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA. Write a function to find all the 10-letter-long seq

[LeetCode] 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 100