Shortest String formed by concatenating string A x times and B y times such that n(A)*x = n(B)*y
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 aaaaaaInput: 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 <bits/stdc++.h>using namespace std;// Function to find GCD of two numbersint 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 Bvoid 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 Codeint main(){ string A = "aaa"; string B = "aa"; ShortestString(A, B); return 0;} |
Java
// Java program for the above approachimport java.util.*;class GFG{// Function to find GCD of two numbersstatic 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 Bstatic 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 Codepublic 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 numbersdef 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 Bdef 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 CodeA = "aaa"B = "aa"ShortestString(A, B)# This code is contributed by Saurabh Jaiswal |
C#
// C# program for the above approachusing System;class GFG{// Function to find GCD of two numbersstatic 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 Bstatic 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 Codepublic static void Main(){ string A = "aaa"; string B = "aa"; ShortestString(A, B);}}// This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript code for the above approach // Function to find GCD of two numbers function 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 function ShortestString(A, B) { let n1 = A.length; let n2 = B.length; let g = gcd(n1, n2); // Stores the Lcm of length // of string A and B let lcm = (n1 * n2) / g; // Stores multiple of string A let C = ""; // Stores multiple of string B let D = ""; // Concatenate A, lcm / n1 times for (let i = 0; i < (lcm / n1); i++) { C = C + A; } // Concatenate B, lcm / n2 times for (let i = 0; i < (lcm / n2); i++) { D = D + B; } // If both strings are equal // to each other if (C == D) { document.write(C); } else { document.write(-1); } } // Driver Code let A = "aaa"; let B = "aa"; ShortestString(A, B);// This code is contributed by Potta Lokesh </script> |
aaaaaa
Time Complexity: O(N)
Auxiliary Space: O(N)


Please Login to comment...