File tree 2 files changed +37
-0
lines changed
2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 4
4
![ problems-solved-java] ( https://img.shields.io/badge/Java-143/1571-1abc9c.svg )
5
5
![ problems-solved-python] ( https://img.shields.io/badge/Python-143/1571-1abc9c.svg )
6
6
[ ![ 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 )
7
8
8
9
🔒 = Subscription Content
9
10
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments