# Repeated String Match LeetCode Solution

Here, We see Repeated String Match LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

# List of all LeetCode Solution

## Problem Statement

Given two strings a and b, return the minimum number of times you should repeat string a so that string b is a substring of it. If it is impossible for b​​​​​​ to be a substring of a after repeating it, return -1.

Notice: string “abc” repeated 0 times is “”, repeated 1 time is “abc” and repeated 2 times is “abcabc”.

Example 1:
Input: a = “abcd”, b = “cdabcdab”
Output: 3
Explanation: We return 3 because by repeating a three times “abcdabcdabcd”, b is a substring of it.

Example 2:
Input: a = “a”, b = “aa”
Output: 2

## Repeated String Match Leetcode Solution C++

``````class Solution {
public:
int repeatedStringMatch(string a, string b) {
string s="";
int count = 0;
while(s.size()<b.size())
{
s+=a;
count++;
}
if(s.find(b)!=string::npos)
return count;
s+=a;
count++;
if(s.find(b)!=string::npos)
return count;
return -1;
}
};```Code language: PHP (php)```

## Repeated String Match Solution Java

``````class Solution {
public int repeatedStringMatch(String a, String b) {
StringBuilder gy=new StringBuilder();
int I=0;
for(I=1; gy.length()<=b.length(); I++){
gy.append(a);
if(gy.toString().contains(b))return I;
}
if(gy.append(a).toString().contains(b))return I;
return -1;
}
}```Code language: JavaScript (javascript)```

## Repeated String Match Solution JavaScript

``````var repeatedStringMatch = function(a, b) {
const count = Math.ceil(b.length / a.length)
const str = a.repeat(count)
return str.includes(b) ? count : (str + a).includes(b) ? count + 1 : -1
};```Code language: JavaScript (javascript)```

## Repeated String Match Solution Python

``````class Solution(object):
def repeatedStringMatch(self, a, b):
if set(b).issubset(set(a)) == False: return -1
for i in range(1,int(len(b)/len(a))+3):
if b in a*i: return i
return -1``````
Scroll to Top