Skip to content

Commit 7e51b4f

Browse files
adds link to cp in readme
1 parent c4321ef commit 7e51b4f

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
![problems-solved-java](https://img.shields.io/badge/Java-143/1571-1abc9c.svg)
55
![problems-solved-python](https://img.shields.io/badge/Python-143/1571-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
7+
[![cp](https://img.shields.io/badge/also%20see-Competitve%20Programming-1f72ff.svg)](https://github.com/anishLearnsToCode/competitive-programming)
78

89
🔒 = Subscription Content
910

Diff for: src/RepeatedStringMatch.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
public class RepeatedStringMatch {
2+
public int repeatedStringMatch(String a, String b) {
3+
Pair result = canBeRepeatedForSubstring(a, b);
4+
if (!result.canBeRepeated) return -1;
5+
return (int) Math.ceil(1 + (double) (a.length() + result.startIndex - b.length()) / b.length());
6+
}
7+
8+
private Pair canBeRepeatedForSubstring(String a, String b) {
9+
boolean checking = false;
10+
char firstChar = a.charAt(0);
11+
int startIndex;
12+
for (int j = 0 ; j < b.length() ; j++) {
13+
if (firstChar == b.charAt(j)) {
14+
int i = 0;
15+
startIndex = j;
16+
for ( ; i < a.length() ; i++, j = (j + 1) % b.length()) {
17+
if (a.charAt(i) != b.charAt(j)) {
18+
break;
19+
}
20+
}
21+
if (i == a.length()) return new Pair(startIndex, true);
22+
}
23+
}
24+
return new Pair(0, false);
25+
}
26+
27+
private static class Pair {
28+
private final int startIndex;
29+
private final boolean canBeRepeated;
30+
31+
Pair(final int startIndex, final boolean canBeRepeated) {
32+
this.startIndex = startIndex;
33+
this.canBeRepeated = canBeRepeated;
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)