# 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++

 `// 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

 ``

Output

`aaaaaa`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up