Skip to content

Commit 991648b

Browse files
committed
feat: add the solution of Repeated String Match(686) with Java.
1 parent ba6e3a7 commit 991648b

File tree

3 files changed

+99
-27
lines changed

3 files changed

+99
-27
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
| [674][674-question] | [Longest Continuous Increasing Subsequence][674-tips] | [][674-java] | [][674-js] | |
7474
| [680][680-question] | [Valid Palindrome II][680-tips] | [][680-java] | [][680-js] | |
7575
| [682][682-question] | [Baseball Game][682-tips] | [][682-java] | [][682-js] | |
76-
| [686][686-question] | [Repeated String Match][686-tips] | | [][686-js] | |
76+
| [686][686-question] | [Repeated String Match][686-tips] | [][686-java] | [][686-js] | |
7777
| [687][687-question] | [Longest Univalue Path][687-tips] | | [][687-js] | |
7878
| [693][693-question] | [Binary Number with Alternating Bits][693-tips] | | [][693-js] | |
7979
| [695][695-question] | [Max Area of Island][695-tips] | | [][695-js] | |
@@ -468,6 +468,7 @@
468468
[671-java]: ./src/_671/Solution.java
469469
[674-java]: ./src/_674/Solution.java
470470
[680-java]: ./src/_680/Solution.java
471+
[686-java]: ./src/_686/Solution.java
471472
[682-java]: ./src/_682/Solution.java
472473
[728-java]: ./src/_728/Solution.java
473474
[771-java]: ./src/_771/Solution.java

src/_686/Solution.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package _686;
2+
3+
public class Solution {
4+
public int repeatedStringMatch(String A, String B) {
5+
if (A.length() > B.length()) {
6+
return A.contains(B) ? 1 : (A + A).contains(B) ? 2 : -1;
7+
}
8+
9+
int aInB = B.indexOf(A);
10+
11+
if (aInB >= A.length()) {
12+
return -1;
13+
}
14+
if (aInB == -1) {
15+
return (A + A).contains(B) ? 2 : -1;
16+
}
17+
18+
int repeatCount = aInB == 0 ? 0 : 1;
19+
int iA = A.length() - aInB;
20+
int iB = 0;
21+
22+
while (iB < B.length()) {
23+
if (iA == A.length()) {
24+
iA = 0;
25+
repeatCount++;
26+
}
27+
28+
if (A.charAt(iA) != B.charAt(iB)) {
29+
return -1;
30+
}
31+
32+
iA++;
33+
iB++;
34+
}
35+
36+
return repeatCount;
37+
}
38+
39+
public static void main(String[] args) {
40+
Solution solution = new Solution();
41+
System.out.println(solution.repeatedStringMatch("abcd", "cdabcdab"));
42+
System.out.println(solution.repeatedStringMatch("abcd", "ab"));
43+
System.out.println(solution.repeatedStringMatch("abcd", "da"));
44+
System.out.println(solution.repeatedStringMatch("a", "aa"));
45+
}
46+
}

tips/686/README.md

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,71 @@
1-
[xxxx][title]
1+
[Repeated String Match][title]
22

33
## Description
4-
// 抄题目
54

5+
Given two strings A and B, find the minimum number of times A has to be repeated such that B is a substring of it. If no such solution, return -1.
66

7-
**Example:**
7+
For example, with A = "abcd" and B = "cdabcdab".
88

9-
```
10-
// 抄Example
11-
```
9+
Return 3, because by repeating A three times (“abcdabcdabcd”), B is a substring of it; and B is not a substring of A repeated two times ("abcdabcd").
1210

1311
**Note:**
14-
// Note
1512

16-
**Tags:** // tags
13+
The length of `A` and `B` will be between 1 and 10000.
1714

15+
**Tags:** [String](https://leetcode.com/tag/string/)
1816

19-
## 思路 1
20-
// 贴一些关键代码,说一些解题思路
21-
// (同一种语言可以写多种思路,与某种语言思路相同的另一种语言的思路无须赘述,但可以把代码贴在后面)
22-
```java
17+
## 思路
2318

24-
```
25-
```javascript
19+
判断重复的A字符串能否使B字符串成为子字符串,若能,计算出至少需要重复几次。使用A字符串对B字符串重复进行匹配即可。但在匹配之前可通过一些已知条件减少一些计算:
2620

27-
```
21+
- 如果A的长度大于B,假如重复A字符串能使B成为子字符串,那么至多只需重复两次。因为如果重复三次才行,那么B的长度肯定大于A,就矛盾了。
22+
- 如果A的长度小于B,用`IndexOf()`方法计算字符串A在B中首次匹配的位置,记作`aInB`
23+
- 如果`aInB`>=`A.length()`,则说明在B的前面一段(比A长或相等)字符串中,没有和A匹配的子字符串,这跟连续的A字符串这个要求不符,因此可以排除。
24+
- 如果`aInB`==`-1`,则B字符串只有可能是两个连续A字符串的子字符串。
25+
- 接着循环用A字符串对B字符串进行匹配。
2826

29-
## 思路 2
30-
// 贴一些关键代码,说一些解题思路
31-
```java
32-
33-
```
34-
35-
## 思路 3
36-
// 贴一些关键代码,说一些解题思路
37-
```kotlin
27+
**Java:**
3828

29+
```java
30+
public int repeatedStringMatch(String A, String B) {
31+
if (A.length() > B.length()) {
32+
return A.contains(B) ? 1 : (A + A).contains(B) ? 2 : -1;
33+
}
34+
35+
int aInB = B.indexOf(A);
36+
37+
if (aInB >= A.length()) {
38+
return -1;
39+
}
40+
if (aInB == -1) {
41+
return (A + A).contains(B) ? 2 : -1;
42+
}
43+
44+
int repeatCount = aInB == 0 ? 0 : 1;
45+
int iA = A.length() - aInB;
46+
int iB = 0;
47+
48+
while (iB < B.length()) {
49+
if (iA == A.length()) {
50+
iA = 0;
51+
repeatCount++;
52+
}
53+
54+
if (A.charAt(iA) != B.charAt(iB)) {
55+
return -1;
56+
}
57+
58+
iA++;
59+
iB++;
60+
}
61+
62+
return repeatCount;
63+
}
3964
```
4065

4166
## 结语
42-
67+
4368
如果你同我们一样热爱数据结构、算法、LeetCode,可以关注我们 GitHub 上的 LeetCode 题解:[LeetCode-Solution][ls]
4469

45-
[title]: https://leetcode.com/problems/xxxx
70+
[title]: https://leetcode.com/problems/repeated-string-match/description/
4671
[ls]: https://github.com/RichCodersAndMe/LeetCode-Solution

0 commit comments

Comments
 (0)