给定两个字符串A和B。任务是找到的最小次数A必须被重复,使得B是它的一个子串。如果没有这样的解决方案,则打印-1 。
例子:
Input : A = “abcd”, B = “cdabcdab”
Output : 3
Repeating A three times (“abcdabcdabcd”), B is a substring of it. B is not a substring of A when it is repeated less than 3 times
Input : A = “ab”, B = “cab”
Output : -1
方法 :
想象一下,我们写了S = A + A + A + …如果B是S的子字符串,我们只需要检查某个索引是0还是1或…。 length(A)-1以B开头,因为S足够长以包含B ,并且S的长度为length(A) 。
现在,假设ans是length(B)<= length(A * ans)的最小数字。我们只需要检查B是A * ans还是A *(ans + 1)的子字符串。如果我们尝试k
下面是上述方法的实现:
C++
// CPP program to find Minimum number of times A
// has to be repeated such that B is a substring of it
#include
using namespace std;
// Function to check if a number
// is a substring of other or not
bool issubstring(string str2, string rep1)
{
int M = str2.length();
int N = rep1.length();
// Check for substring from starting
// from i'th index of main string
for (int i = 0; i <= N - M; i++) {
int j;
// For current index i,
// check for pattern match
for (j = 0; j < M; j++)
if (rep1[i + j] != str2[j])
break;
if (j == M) // pattern matched
return true;
}
return false; // not a substring
}
// Function to find Minimum number of times A
// has to be repeated such that B is a substring of it
int Min_repetation(string A, string B)
{
// To store minimum number of repetations
int ans = 1;
// To store repeated string
string S = A;
// Untill size of S is less than B
while(S.size() < B.size())
{
S += A;
ans++;
}
// ans times repetation makes required answer
if (issubstring(B, S)) return ans;
// Add one more string of A
if (issubstring(B, S+A))
return ans + 1;
// If no such solution exits
return -1;
}
// Driver code
int main()
{
string A = "abcd", B = "cdabcdab";
// Function call
cout << Min_repetation(A, B);
return 0;
}
Java
// Java program to find minimum number
// of times 'A' has to be repeated
// such that 'B' is a substring of it
class GFG
{
// Function to check if a number
// is a substring of other or not
static boolean issubstring(String str2,
String rep1)
{
int M = str2.length();
int N = rep1.length();
// Check for substring from starting
// from i'th index of main string
for (int i = 0; i <= N - M; i++)
{
int j;
// For current index i,
// check for pattern match
for (j = 0; j < M; j++)
if (rep1.charAt(i + j) != str2.charAt(j))
break;
if (j == M) // pattern matched
return true;
}
return false; // not a substring
}
// Function to find Minimum number
// of times 'A' has to be repeated
// such that 'B' is a substring of it
static int Min_repetation(String A, String B)
{
// To store minimum number of repetations
int ans = 1;
// To store repeated string
String S = A;
// Untill size of S is less than B
while(S.length() < B.length())
{
S += A;
ans++;
}
// ans times repetation makes required answer
if (issubstring(B, S)) return ans;
// Add one more string of A
if (issubstring(B, S + A))
return ans + 1;
// If no such solution exits
return -1;
}
// Driver code
public static void main(String[] args)
{
String A = "abcd", B = "cdabcdab";
// Function call
System.out.println(Min_repetation(A, B));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to find minimum number
# of times 'A' has to be repeated
# such that 'B' is a substring of it
# Methof to find Minimum number
# of times 'A' has to be repeated
# such that 'B' is a substring of it
def min_repetitions(a, b):
len_a = len(a)
len_b = len(b)
for i in range(0, len_a):
if a[i] == b[0]:
k = i
count = 1
for j in range(0, len_b):
# we are reiterating over A again and
# again for each value of B
# Resetting A pointer back to 0 as B
# is not empty yet
if k >= len_a:
k = 0
count = count + 1
# Resetting A means count
# needs to be increased
if a[k] != b[j]:
break
k = k + 1
# k is iterating over A
else:
return count
return -1
# Driver Code
A = 'abcd'
B = 'cdabcdab'
print(min_repetitions(A, B))
# This code is contributed by satycool
C#
// C# program to find minimum number
// of times 'A' has to be repeated
// such that 'B' is a substring of it
using System;
class GFG
{
// Function to check if a number
// is a substring of other or not
static Boolean issubstring(String str2,
String rep1)
{
int M = str2.Length;
int N = rep1.Length;
// Check for substring from starting
// from i'th index of main string
for (int i = 0; i <= N - M; i++)
{
int j;
// For current index i,
// check for pattern match
for (j = 0; j < M; j++)
if (rep1[i + j] != str2[j])
break;
if (j == M) // pattern matched
return true;
}
return false; // not a substring
}
// Function to find Minimum number
// of times 'A' has to be repeated
// such that 'B' is a substring of it
static int Min_repetation(String A, String B)
{
// To store minimum number of repetations
int ans = 1;
// To store repeated string
String S = A;
// Untill size of S is less than B
while(S.Length < B.Length)
{
S += A;
ans++;
}
// ans times repetation makes required answer
if (issubstring(B, S)) return ans;
// Add one more string of A
if (issubstring(B, S + A))
return ans + 1;
// If no such solution exits
return -1;
}
// Driver code
public static void Main(String[] args)
{
String A = "abcd", B = "cdabcdab";
// Function call
Console.WriteLine(Min_repetation(A, B));
}
}
// This code is contributed by 29AjayKumar
输出:
3