Skip to content

Commit 454c030

Browse files
committed
feat: add solutions to lc problem: No.0459
No.0459.Repeated Substring Pattern
1 parent 5fc6792 commit 454c030

File tree

5 files changed

+62
-2
lines changed

5 files changed

+62
-2
lines changed

solution/0400-0499/0459.Repeated Substring Pattern/README.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,22 @@
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
**方法一:双倍字符串**
52+
53+
若长度为 $n$ 的字符串 `s` 由 $m$ 个重复子串组成,将 `s` 拼接在自身上,得到字符串 `ss`,长度为 $2n$,此时若从下标 `1` 开始查找 `s`,那么查找到的下标一定小于 `s.length`
54+
55+
若长度为 $n$ 的字符串 `s` 不由重复子串组成,将 `s` 拼接在自身上,得到字符串 `ss`,长度为 $2n$,此时若从下标 `1` 开始查找 `s`,那么查找到的下标一定等于 `s.length`
56+
5157
<!-- tabs:start -->
5258

5359
### **Python3**
5460

5561
<!-- 这里可写当前语言的特殊实现逻辑 -->
5662

5763
```python
58-
64+
class Solution:
65+
def repeatedSubstringPattern(self, s: str) -> bool:
66+
return (s + s).index(s, 1) < len(s)
5967
```
6068

6169
### **Java**
@@ -71,6 +79,25 @@ class Solution {
7179
}
7280
```
7381

82+
### **C++**
83+
84+
```cpp
85+
class Solution {
86+
public:
87+
bool repeatedSubstringPattern(string s) {
88+
return (s + s).find(s, 1) < s.size();
89+
}
90+
};
91+
```
92+
93+
### **Go**
94+
95+
```go
96+
func repeatedSubstringPattern(s string) bool {
97+
return strings.Index(s[1:]+s, s) < len(s)-1
98+
}
99+
```
100+
74101
### **TypeScript**
75102

76103
```ts

solution/0400-0499/0459.Repeated Substring Pattern/README_EN.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
### **Python3**
4646

4747
```python
48-
48+
class Solution:
49+
def repeatedSubstringPattern(self, s: str) -> bool:
50+
return (s + s).index(s, 1) < len(s)
4951
```
5052

5153
### **Java**
@@ -59,6 +61,25 @@ class Solution {
5961
}
6062
```
6163

64+
### **C++**
65+
66+
```cpp
67+
class Solution {
68+
public:
69+
bool repeatedSubstringPattern(string s) {
70+
return (s + s).find(s, 1) < s.size();
71+
}
72+
};
73+
```
74+
75+
### **Go**
76+
77+
```go
78+
func repeatedSubstringPattern(s string) bool {
79+
return strings.Index(s[1:]+s, s) < len(s)-1
80+
}
81+
```
82+
6283
### **TypeScript**
6384

6485
```ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
bool repeatedSubstringPattern(string s) {
4+
return (s + s).find(s, 1) < s.size();
5+
}
6+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func repeatedSubstringPattern(s string) bool {
2+
return strings.Index(s[1:]+s, s) < len(s)-1
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def repeatedSubstringPattern(self, s: str) -> bool:
3+
return (s + s).index(s, 1) < len(s)

0 commit comments

Comments
 (0)