由连接字符串A x 次和 B y 次形成的最短字符串,使得 n(A)*x = n(B)*y
给定两个字符串A和B ,任务是找到最短的字符串,它是A和B的倍数。如果字符串X可以由字符串Y的多次出现串联形成,则称字符串X是字符串Y的倍数。
例子:
Input: A = “aaa”, B= “aa”
Output: aaaaaa
Explanation: Multiplying A two times and B three times will result in aaaaaa
Input: A=”cold”, B =”old”
Output: -1
方法:可以基于以下观察来解决给定的问题:可以是字符串A和B的倍数的最小字符串的长度必须等于A的长度和B的长度的 LCM。因此,可以使用以下步骤解决给定的问题:
- 创建一个变量lcm ,其中存储了A的长度和B的长度的 LCM 值。
- 创建一个字符串C ,它是在连接字符串A 、 lcm / (A 的长度)次之后形成的。
- 类似地,创建一个字符串D ,它是在连接字符串B 、 lcm /(B 的长度)次之后形成的。
- 检查字符串C和D是否相等。如果是,则打印其中任何一个,否则打印-1 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find GCD of two numbers
int gcd(int a, int b)
{
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
// Function to find shortest string that
// is a multiple of string A and B
void ShortestString(string A, string B)
{
int n1 = A.length();
int n2 = B.length();
int g = gcd(n1, n2);
// Stores the Lcm of length
// of string A and B
int lcm = (n1 * n2) / g;
// Stores multiple of string A
string C = "";
// Stores multiple of string B
string D = "";
// Concatenate A, lcm / n1 times
for (int i = 0; i < (lcm / n1); i++) {
C = C + A;
}
// Concatenate B, lcm / n2 times
for (int i = 0; i < (lcm / n2); i++) {
D = D + B;
}
// If both strings are equal
// to each other
if (C == D) {
cout << C;
}
else {
cout << -1;
}
}
// Driver Code
int main()
{
string A = "aaa";
string B = "aa";
ShortestString(A, B);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find GCD of two numbers
static int gcd(int a, int b)
{
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
// Function to find shortest String that
// is a multiple of String A and B
static void ShortestString(String A, String B)
{
int n1 = A.length();
int n2 = B.length();
int g = gcd(n1, n2);
// Stores the Lcm of length
// of String A and B
int lcm = (n1 * n2) / g;
// Stores multiple of String A
String C = "";
// Stores multiple of String B
String D = "";
// Concatenate A, lcm / n1 times
for (int i = 0; i < (lcm / n1); i++) {
C = C + A;
}
// Concatenate B, lcm / n2 times
for (int i = 0; i < (lcm / n2); i++) {
D = D + B;
}
// If both Strings are equal
// to each other
if (C.equals(D)) {
System.out.print(C);
}
else {
System.out.print(-1);
}
}
// Driver Code
public static void main(String[] args)
{
String A = "aaa";
String B = "aa";
ShortestString(A, B);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python code for the above approach
# Function to find GCD of two numbers
def gcd(a, b):
if (a == 0):
return b
return gcd(b % a, a)
# Function to find shortest string that
# is a multiple of string A and B
def ShortestString(A, B):
n1 = len(A)
n2 = len(B)
g = gcd(n1, n2)
# Stores the Lcm of length
# of string A and B
lcm = (n1 * n2) / g
# Stores multiple of string A
C = ""
# Stores multiple of string B
D = ""
# Concatenate A, lcm / n1 times
for i in range(0, (int)(lcm//n1)):
C = C + A
# Concatenate B, lcm / n2 times
for i in range((int)(lcm // n2)):
D = D + B
# If both strings are equal
# to each other
if (C == D):
print(C)
else:
print(-1)
# Driver Code
A = "aaa"
B = "aa"
ShortestString(A, B)
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG{
// Function to find GCD of two numbers
static int gcd(int a, int b)
{
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
// Function to find shortest String that
// is a multiple of String A and B
static void ShortestString(string A, string B)
{
int n1 = A.Length;
int n2 = B.Length;
int g = gcd(n1, n2);
// Stores the Lcm of length
// of String A and B
int lcm = (n1 * n2) / g;
// Stores multiple of String A
string C = "";
// Stores multiple of String B
string D = "";
// Concatenate A, lcm / n1 times
for (int i = 0; i < (lcm / n1); i++) {
C = C + A;
}
// Concatenate B, lcm / n2 times
for (int i = 0; i < (lcm / n2); i++) {
D = D + B;
}
// If both Strings are equal
// to each other
if (C.Equals(D)) {
Console.Write(C);
}
else {
Console.Write(-1);
}
}
// Driver Code
public static void Main()
{
string A = "aaa";
string B = "aa";
ShortestString(A, B);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
aaaaaa
时间复杂度: O(N)
辅助空间: O(1)