File tree 5 files changed +62
-2
lines changed
solution/0400-0499/0459.Repeated Substring Pattern
5 files changed +62
-2
lines changed Original file line number Diff line number Diff line change 48
48
49
49
<!-- 这里可写通用的实现逻辑 -->
50
50
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
+
51
57
<!-- tabs:start -->
52
58
53
59
### ** Python3**
54
60
55
61
<!-- 这里可写当前语言的特殊实现逻辑 -->
56
62
57
63
``` python
58
-
64
+ class Solution :
65
+ def repeatedSubstringPattern (self , s : str ) -> bool :
66
+ return (s + s).index(s, 1 ) < len (s)
59
67
```
60
68
61
69
### ** Java**
@@ -71,6 +79,25 @@ class Solution {
71
79
}
72
80
```
73
81
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
+
74
101
### ** TypeScript**
75
102
76
103
``` ts
Original file line number Diff line number Diff line change 45
45
### ** Python3**
46
46
47
47
``` python
48
-
48
+ class Solution :
49
+ def repeatedSubstringPattern (self , s : str ) -> bool :
50
+ return (s + s).index(s, 1 ) < len (s)
49
51
```
50
52
51
53
### ** Java**
@@ -59,6 +61,25 @@ class Solution {
59
61
}
60
62
```
61
63
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
+
62
83
### ** TypeScript**
63
84
64
85
``` ts
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool repeatedSubstringPattern (string s) {
4
+ return (s + s).find (s, 1 ) < s.size ();
5
+ }
6
+ };
Original file line number Diff line number Diff line change
1
+ func repeatedSubstringPattern (s string ) bool {
2
+ return strings .Index (s [1 :]+ s , s ) < len (s )- 1
3
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def repeatedSubstringPattern (self , s : str ) -> bool :
3
+ return (s + s ).index (s , 1 ) < len (s )
You can’t perform that action at this time.
0 commit comments