Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita552963

Browse files
committed
459-repeated-substring-pattern.md Added Python, JavaScript solutions.
1 parentafc35c0 commita552963

File tree

3 files changed

+122
-2
lines changed

3 files changed

+122
-2
lines changed

‎README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ You can skip the more difficult problems and do them later.
4242
#String
4343
-[28. Find the Index of the First Occurrence in a String](solutions/1-1000/28-find-the-index-of-the-first-occurrence-in-a-string.md) was solved in_Python, JavaScript_.
4444
-[541. Reverse String II](solutions/1-1000/541-reverse-string-ii.md) was solved in_Python, Java, JavaScript, C#, Ruby_.
45+
-[459. Repeated Substring Pattern](solutions/1-1000/459-repeated-substring-pattern.md)
4546

4647
#Two Pointers
4748
-[18. 4Sum](solutions/1-1000/18-4sum.md) was solved in_Python_.
@@ -77,6 +78,8 @@ You can skip the more difficult problems and do them later.
7778
-[188. Best Time to Buy and Sell Stock IV](solutions/1-1000/188-best-time-to-buy-and-sell-stock-iv.md) was solved in_Python, JavaScript, Go_.
7879
-[309. Best Time to Buy and Sell Stock with Cooldown](solutions/1-1000/309-best-time-to-buy-and-sell-stock-with-cooldown.md) was solved in_Python, JavaScript, Go_.
7980

81+
to here now
82+
8083
##Subsequence Problems
8184
-[674. Longest Continuous Increasing Subsequence](solutions/1-1000/674-longest-continuous-increasing-subsequence.md) was solved in_Python, Java, JavaScript, C#_.
8285
-[300. Longest Increasing Subsequence](solutions/1-1000/300-longest-increasing-subsequence.md) was solved in_Python, Java, JavaScript, C#_.

‎solutions/1-1000/28-find-the-index-of-the-first-occurrence-in-a-string.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#28. Find the Index of the First Occurrence in a String - LeetCode Solution
2-
LeetCode English link:[28. Find the Index of the First Occurrence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string),
2+
LeetCode English link:[28. Find the Index of the First Occurrence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string)
33

44
LeetCode Chinese link:[28. 找出字符串中第一个匹配项的下标](https://leetcode.cn/problems/find-the-index-of-the-first-occurrence-in-a-string)
5-
([中文题解](#中文题解))
5+
6+
[中文题解](#中文题解)
67

78
##LeetCode problem description
89
Given two strings`needle` and`haystack`, return the**index** of the first occurrence of`needle` in`haystack`, or`-1` if`needle` is not part of`haystack`.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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]`。想明白了这一点,子串的排查范围就大大缩小了。

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp