Shortest String formed by concatenating string A x times and B y times such that n(A)*x = n(B)*y
Last Updated :
06 Feb, 2023
Given two strings A and B, the task is to find the shortest string which is some multiple of both A and B. A string X is said to be a multiple of string Y if string X can be formed by the concatenation of multiple occurrences of string Y.
Example:
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
Approach: The given problem can be solved based on the observation that the length of the smallest string that can be a multiple of both the strings A and B must be equal to the LCM of the length of A and length of B. Therefore, the given problem can be solved using the following steps:
- Create a variable lcm, which stores the LCM value of the length of A and length of B.
- Create a string C which is formed after concatenating string A, lcm / (length of A) times.
- Similarly, create a string D which is formed after concatenating string B, lcm / (length of B) times.
- Check if strings C and D are equal or not. If they are, print any of them otherwise print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
void ShortestString(string A, string B)
{
int n1 = A.length();
int n2 = B.length();
int g = gcd(n1, n2);
int lcm = (n1 * n2) / g;
string C = "" ;
string D = "" ;
for ( int i = 0; i < (lcm / n1); i++) {
C = C + A;
}
for ( int i = 0; i < (lcm / n2); i++) {
D = D + B;
}
if (C == D) {
cout << C;
}
else {
cout << -1;
}
}
int main()
{
string A = "aaa" ;
string B = "aa" ;
ShortestString(A, B);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int gcd( int a, int b)
{
if (a == 0 ) {
return b;
}
return gcd(b % a, a);
}
static void ShortestString(String A, String B)
{
int n1 = A.length();
int n2 = B.length();
int g = gcd(n1, n2);
int lcm = (n1 * n2) / g;
String C = "" ;
String D = "" ;
for ( int i = 0 ; i < (lcm / n1); i++) {
C = C + A;
}
for ( int i = 0 ; i < (lcm / n2); i++) {
D = D + B;
}
if (C.equals(D)) {
System.out.print(C);
}
else {
System.out.print(- 1 );
}
}
public static void main(String[] args)
{
String A = "aaa" ;
String B = "aa" ;
ShortestString(A, B);
}
}
|
Python3
def gcd(a, b):
if (a = = 0 ):
return b
return gcd(b % a, a)
def ShortestString(A, B):
n1 = len (A)
n2 = len (B)
g = gcd(n1, n2)
lcm = (n1 * n2) / g
C = ""
D = ""
for i in range ( 0 , ( int )(lcm / / n1)):
C = C + A
for i in range (( int )(lcm / / n2)):
D = D + B
if (C = = D):
print (C)
else :
print ( - 1 )
A = "aaa"
B = "aa"
ShortestString(A, B)
|
C#
using System;
class GFG{
static int gcd( int a, int b)
{
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
static void ShortestString( string A, string B)
{
int n1 = A.Length;
int n2 = B.Length;
int g = gcd(n1, n2);
int lcm = (n1 * n2) / g;
string C = "" ;
string D = "" ;
for ( int i = 0; i < (lcm / n1); i++) {
C = C + A;
}
for ( int i = 0; i < (lcm / n2); i++) {
D = D + B;
}
if (C.Equals(D)) {
Console.Write(C);
}
else {
Console.Write(-1);
}
}
public static void Main()
{
string A = "aaa" ;
string B = "aa" ;
ShortestString(A, B);
}
}
|
Javascript
<script>
function gcd(a, b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
function ShortestString(A, B) {
let n1 = A.length;
let n2 = B.length;
let g = gcd(n1, n2);
let lcm = (n1 * n2) / g;
let C = "" ;
let D = "" ;
for (let i = 0; i < (lcm / n1); i++) {
C = C + A;
}
for (let i = 0; i < (lcm / n2); i++) {
D = D + B;
}
if (C == D) {
document.write(C);
}
else {
document.write(-1);
}
}
let A = "aaa" ;
let B = "aa" ;
ShortestString(A, B);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...