Shortest String formed by Concatenating String A x Times and B y Times such that n(A)\x = n(B)*y


In this problem, we need to find the shortest string, which is a multiple of string A and string B. The problem is very similar to finding the LCM (Least common multiplier) of two numbers. We can find the LCM of both strings’ lengths and make both strings’ lengths equal to LCM by concatenating to itself. After that, we can compare the string to check if it is possible to get the shortest string which multiple of A and B.

Problem statement – We have given string A and string B of different lengths. We need to find the shortest string, which is a multiple of string A and string B.

Note – The first string is multiple of the second string if we can achieve the second string by concatenating the first string to itself multiple times.

Sample examples

Input

A = "ddd", B = "d";

Output

‘ddd’

Explanation – The ‘ddd’ string is a multiple of A and a multiple of B as 1*A = 3*B.

Input

A = "abc", B = "def";

Output

-1

Explanation – It is not possible to find the shortest string, which is a multiple of A and B, as both strings are different.

Input

A = "abcabcabc", B = "abcabc";

Output

‘abcabcabcabcabcabc’

Explanation – The shorted string, a multiple of A and B, is shown in the output as 2*A == 3*B.

Approach 1

This approach will find the GCD of both strings’ lengths. After that, we will find LCM (the least common multiplier) using the GCD. Based on the LCM value, we will concatenate both strings to themselves multiple times and match the final string to check whether they are equal.

Algorithm

Step 1 – Define the getGCD() function to find the GCD of two numbers, which takes the first and second as parameters.

Step 1.1 – If the first is equal to zero, return the second.

Step 1.2 – Take the modulo of the second with the first and pass it as a first parameter of the GCD function while making the recursive call.

Step 2 – In the multipleOfAandB() function, initialize the len1 and len2 variables with the length of the string A and string B.

Step 3 – Execute the getGCD() function to get the GCDof two numbers and store it in the gcd variable.

Step 4 – Next, multiply len1 and len2 and divide them with the GCD value to get the LCM value.

Step 5 – Initialize two temporary strings named temp1 and temp2 to store the multiplied strings.

Step 6 – Append the string A to the temp1 for LCM/len1 times.

Step 7 – Append the string B to the temp2 for LCM/len2 times.

Step 8 – If temp1 and temp2 are equal, print the value of temp1 or temp2. Else, print -1.

Example

#include <bits/stdc++.h>
using namespace std;

// Get GCD of numbers
int getGCD(int first, int second){
    if (first == 0) {
        return second;
    }
    return getGCD(second % first, first);
}
void multipleOfAandB(string A, string B){
    int len1 = A.length();
    int len2 = B.length();
    int gcd = getGCD(len1, len2);
    // Get LCM of len1 and len2
    int lcm = (len1 * len2) / gcd;
    // To store multiple of A
    string temp1 = "";
    // To store multiple of B
    string temp2 = "";
    // string concatenation
    for (int m = 0; m < (lcm / len1); m++) {
        temp1 = temp1 + A;
    }
    // string concatenation
    for (int m = 0; m < (lcm / len2); m++) {
        temp2 = temp2 + B;
    }
    // Compare temp1 and temp2
    if (temp1 == temp2) {
        cout << "The resultnat string is - " << temp1;
    } else {
        cout << -1;
    }
}
int main() {
    string A = "ddd";
    string B = "d";
    multipleOfAandB(A, B);
    return 0;
}

Output

The resultnat string is - ddd

Time complexity – O(log(N*M)) as we find the GCD of lengths and concatenate the string

Space complexity – O(N + M), as we required to store the shortest string.

The solution to the problem was very similar to the finding LCM of two numbers, but we need to make sure here that the string should be similar after concatenating multiple times.

Updated on: 25-Aug-2023

46 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements