|
| 1 | +#459. Repeated Substring Pattern - LeetCode Solution |
| 2 | +LeetCode English link:[459. Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern) |
| 3 | + |
| 4 | +LeetCode Chinese link:[中文问题名称](https://leetcode.cn/problems/repeated-substring-pattern) |
| 5 | + |
| 6 | +[中文题解](#中文题解) |
| 7 | + |
| 8 | +##LeetCode problem description |
| 9 | +Given a string`s`, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. |
| 10 | + |
| 11 | +Difficulty:**Easy** |
| 12 | + |
| 13 | +###[Example 1] |
| 14 | +**Input**:`s = "abcabcabcabc"` |
| 15 | + |
| 16 | +**Output**:`true` |
| 17 | + |
| 18 | +**Explanation**:`It is the substring "abc" four times or the substring "abcabc" twice.` |
| 19 | + |
| 20 | +###[Example 2] |
| 21 | +**Input**:`s = "aba"` |
| 22 | + |
| 23 | +**Output**:`false` |
| 24 | + |
| 25 | +###[Constraints] |
| 26 | +-`1 <= s.length <= 10000` |
| 27 | +-`s` consists of lowercase English letters. |
| 28 | + |
| 29 | +##Intuition behind the Solution |
| 30 | +[中文题解](#中文题解) |
| 31 | + |
| 32 | +The key to solving this problem is to see clearly that if`s` can be obtained by repeating the substring, then the starting letter of the substring must be`s[0]`. |
| 33 | +Once you understand this, the scope of substring investigation is greatly narrowed. |
| 34 | + |
| 35 | +##Complexity |
| 36 | +* Time:`O(N * N)`. |
| 37 | +* Space:`O(N)`. |
| 38 | + |
| 39 | +##Python |
| 40 | +```python |
| 41 | +classSolution: |
| 42 | +defrepeatedSubstringPattern(self,s:str) ->bool: |
| 43 | +for iinrange(1,int(len(s)/2)+1): |
| 44 | +iflen(s)% i==0and s[:i]*int(len(s)/ i)== s: |
| 45 | +returnTrue |
| 46 | + |
| 47 | +returnFalse |
| 48 | +``` |
| 49 | + |
| 50 | +##C++ |
| 51 | +```cpp |
| 52 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 53 | +``` |
| 54 | + |
| 55 | +##JavaScript |
| 56 | +```javascript |
| 57 | +varrepeatedSubstringPattern=function (s) { |
| 58 | +for (let i=1; i<=s.length/2; i++) { |
| 59 | +if (s.length% i!=0) { |
| 60 | +continue |
| 61 | + } |
| 62 | + |
| 63 | +if (s.slice(0, i).repeat(s.length/ i)== s) { |
| 64 | +returntrue |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | +returnfalse |
| 69 | +}; |
| 70 | +``` |
| 71 | + |
| 72 | +##Java |
| 73 | +```java |
| 74 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 75 | +``` |
| 76 | + |
| 77 | +##C# |
| 78 | +```c# |
| 79 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 80 | +``` |
| 81 | + |
| 82 | +##Go |
| 83 | +```go |
| 84 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 85 | +``` |
| 86 | + |
| 87 | +##Ruby |
| 88 | +```ruby |
| 89 | +# Welcome to create a PR to complete the code of this language, thanks! |
| 90 | +``` |
| 91 | + |
| 92 | +##C, Kotlin, Swift, Rust or other languages |
| 93 | +``` |
| 94 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 95 | +``` |
| 96 | + |
| 97 | +##力扣“459. 重复的子字符串”问题描述 |
| 98 | +力扣链接:[459. 重复的子字符串](https://leetcode.cn/problems/repeated-substring-pattern), 难度:**简单**。 |
| 99 | + |
| 100 | +给定一个非空的字符串`s` ,检查是否可以通过由它的一个子串重复多次构成。 |
| 101 | + |
| 102 | +###[示例 1] |
| 103 | +**输入**:`s = "abcabcabcabc"` |
| 104 | + |
| 105 | +**输出**:`true` |
| 106 | + |
| 107 | +**解释**:`可由子串 "abc" 重复四次构成。 (或子串 "abcabc" 重复两次构成。)` |
| 108 | + |
| 109 | +###[示例 2] |
| 110 | +**输入**:`s = "aba"` |
| 111 | + |
| 112 | +**输出**:`false` |
| 113 | + |
| 114 | +#中文题解 |
| 115 | +##思路 |
| 116 | +解决本问题的关键是要看清楚一点:通过子串的重复能得到`s`,那么子串的起始字母一定是`s[0]`。想明白了这一点,子串的排查范围就大大缩小了。 |