Skip to content
loading...

Repeated String Match - #Challenge

twitter logo github logo ・1 min read  

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.

For example, with A = "abcd" and B = "cdabcdab".

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").

Note:
The length of A and B will be between 1 and 10000.

My Solution:

class Solution {
public int repeatedStringMatch(String A, String B) {
int count = 1;
String s = A;
while(s.length()<=10000){
if(s.indexOf(B)!=-1){
return count;
}else{
++count;
s=s+A;
}
}
return -1;
}
}

Happy Coding! 😇 please share your approach on comments...

twitter logo DISCUSS
Classic DEV Post from Sep 17 '19

SOLID PRINCIPLES: To start with Object-oriented programming

kumaresan kandasamy profile image
Tech Blogger