File tree 6 files changed +185
-4
lines changed
solution/0600-0699/0686.Repeated String Match
6 files changed +185
-4
lines changed Original file line number Diff line number Diff line change 47
47
<li><code>a</code> 和 <code>b</code> 由小写英文字母组成</li>
48
48
</ul >
49
49
50
-
51
50
## 解法
52
51
53
52
<!-- 这里可写通用的实现逻辑 -->
54
53
54
+ 新串由 a 通过重复最小 ans 次得来,假设新串包含子字符串 b,那么新串的起始匹配位置一定不超过 a。
55
+
55
56
<!-- tabs:start -->
56
57
57
58
### ** Python3**
58
59
59
60
<!-- 这里可写当前语言的特殊实现逻辑 -->
60
61
61
62
``` python
62
-
63
+ class Solution :
64
+ def repeatedStringMatch (self , a : str , b : str ) -> int :
65
+ m, n = len (a), len (b)
66
+ ans = ceil(n / m)
67
+ t = [a] * ans
68
+ for _ in range (3 ):
69
+ if b in ' ' .join(t):
70
+ return ans
71
+ ans += 1
72
+ t.append(a)
73
+ return - 1
63
74
```
64
75
65
76
### ** Java**
66
77
67
78
<!-- 这里可写当前语言的特殊实现逻辑 -->
68
79
69
80
``` java
81
+ class Solution {
82
+ public int repeatedStringMatch (String a , String b ) {
83
+ int m = a. length(), n = b. length();
84
+ int ans = (n + m - 1 ) / m;
85
+ StringBuilder t = new StringBuilder (a. repeat(ans));
86
+ for (int i = 0 ; i < 3 ; ++ i) {
87
+ if (t. toString(). contains(b)) {
88
+ return ans;
89
+ }
90
+ ++ ans;
91
+ t. append(a);
92
+ }
93
+ return - 1 ;
94
+ }
95
+ }
96
+ ```
97
+
98
+ ### ** C++**
99
+
100
+ ``` cpp
101
+ class Solution {
102
+ public:
103
+ int repeatedStringMatch(string a, string b) {
104
+ int m = a.size(), n = b.size();
105
+ int ans = (n + m - 1) / m;
106
+ string t = "";
107
+ for (int i = 0; i < ans; ++i) t += a;
108
+ for (int i = 0; i < 3; ++i)
109
+ {
110
+ if (t.find(b) != -1) return ans;
111
+ ++ans;
112
+ t += a;
113
+ }
114
+ return -1;
115
+ }
116
+ };
117
+ ```
70
118
119
+ ### **Go**
120
+
121
+ ```go
122
+ func repeatedStringMatch(a string, b string) int {
123
+ m, n := len(a), len(b)
124
+ ans := (n + m - 1) / m
125
+ t := strings.Repeat(a, ans)
126
+ for i := 0; i < 3; i++ {
127
+ if strings.Contains(t, b) {
128
+ return ans
129
+ }
130
+ ans++
131
+ t += a
132
+ }
133
+ return -1
134
+ }
71
135
```
72
136
73
137
### ** ...**
Original file line number Diff line number Diff line change 47
47
<li><code>a</code> and <code>b</code> consist of lower-case English letters.</li>
48
48
</ul >
49
49
50
-
51
50
## Solutions
52
51
53
52
<!-- tabs:start -->
54
53
55
54
### ** Python3**
56
55
57
56
``` python
58
-
57
+ class Solution :
58
+ def repeatedStringMatch (self , a : str , b : str ) -> int :
59
+ m, n = len (a), len (b)
60
+ ans = ceil(n / m)
61
+ t = [a] * ans
62
+ for _ in range (3 ):
63
+ if b in ' ' .join(t):
64
+ return ans
65
+ ans += 1
66
+ t.append(a)
67
+ return - 1
59
68
```
60
69
61
70
### ** Java**
62
71
63
72
``` java
73
+ class Solution {
74
+ public int repeatedStringMatch (String a , String b ) {
75
+ int m = a. length(), n = b. length();
76
+ int ans = (n + m - 1 ) / m;
77
+ StringBuilder t = new StringBuilder (a. repeat(ans));
78
+ for (int i = 0 ; i < 3 ; ++ i) {
79
+ if (t. toString(). contains(b)) {
80
+ return ans;
81
+ }
82
+ ++ ans;
83
+ t. append(a);
84
+ }
85
+ return - 1 ;
86
+ }
87
+ }
88
+ ```
89
+
90
+ ### ** C++**
91
+
92
+ ``` cpp
93
+ class Solution {
94
+ public:
95
+ int repeatedStringMatch(string a, string b) {
96
+ int m = a.size(), n = b.size();
97
+ int ans = (n + m - 1) / m;
98
+ string t = "";
99
+ for (int i = 0; i < ans; ++i) t += a;
100
+ for (int i = 0; i < 3; ++i)
101
+ {
102
+ if (t.find(b) != -1) return ans;
103
+ ++ans;
104
+ t += a;
105
+ }
106
+ return -1;
107
+ }
108
+ };
109
+ ```
64
110
111
+ ### **Go**
112
+
113
+ ```go
114
+ func repeatedStringMatch(a string, b string) int {
115
+ m, n := len(a), len(b)
116
+ ans := (n + m - 1) / m
117
+ t := strings.Repeat(a, ans)
118
+ for i := 0; i < 3; i++ {
119
+ if strings.Contains(t, b) {
120
+ return ans
121
+ }
122
+ ans++
123
+ t += a
124
+ }
125
+ return -1
126
+ }
65
127
```
66
128
67
129
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int repeatedStringMatch (string a, string b) {
4
+ int m = a.size (), n = b.size ();
5
+ int ans = (n + m - 1 ) / m;
6
+ string t = " " ;
7
+ for (int i = 0 ; i < ans; ++i) t += a;
8
+ for (int i = 0 ; i < 3 ; ++i)
9
+ {
10
+ if (t.find (b) != -1 ) return ans;
11
+ ++ans;
12
+ t += a;
13
+ }
14
+ return -1 ;
15
+ }
16
+ };
Original file line number Diff line number Diff line change
1
+ func repeatedStringMatch (a string , b string ) int {
2
+ m , n := len (a ), len (b )
3
+ ans := (n + m - 1 ) / m
4
+ t := strings .Repeat (a , ans )
5
+ for i := 0 ; i < 3 ; i ++ {
6
+ if strings .Contains (t , b ) {
7
+ return ans
8
+ }
9
+ ans ++
10
+ t += a
11
+ }
12
+ return - 1
13
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int repeatedStringMatch (String a , String b ) {
3
+ int m = a .length (), n = b .length ();
4
+ int ans = (n + m - 1 ) / m ;
5
+ StringBuilder t = new StringBuilder (a .repeat (ans ));
6
+ for (int i = 0 ; i < 3 ; ++i ) {
7
+ if (t .toString ().contains (b )) {
8
+ return ans ;
9
+ }
10
+ ++ans ;
11
+ t .append (a );
12
+ }
13
+ return -1 ;
14
+ }
15
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def repeatedStringMatch (self , a : str , b : str ) -> int :
3
+ m , n = len (a ), len (b )
4
+ ans = ceil (n / m )
5
+ t = [a ] * ans
6
+ for _ in range (3 ):
7
+ if b in '' .join (t ):
8
+ return ans
9
+ ans += 1
10
+ t .append (a )
11
+ return - 1
You can’t perform that action at this time.
0 commit comments