Smallest string divisible by two given strings
Last Updated :
30 Jun, 2021
Given two strings S and T of length N and M respectively, the task is to find the smallest string that is divisible by both the two strings. If no such string exists, then print -1.
For any two strings A and B, B divides A if and only if A is the concatenation of B at least once.
Examples:
Input: S = “abab”, T = “ab”
Output: abab
Explanation: The string “abab” is the same as S and twice the concatenation of string T (“abab” = “ab” + “ab” = T + T)
Input: S = “ccc”, T = “cc”
Output: cccccc
Explanation: The string “cccccc” is a concatenation of S and T twice and thrice respectively.
(“cccccc” = “ccc” + “ccc” = S + S)
(“cccccc” = “cc” + “cc” + “cc” = T + T + T)
Approach: The idea is based on the observation that the length of the required string, say, L, must be equal to the least common multiple of N and M. Check if string S concatenated L / N number of times is equal to string T being concatenated L / M number of times or not. If found to be true, print any one of them. Otherwise, print -1. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int lcm( int a, int b)
{
return (a / gcd(a, b)) * b;
}
void findSmallestString(string s, string t)
{
int n = s.length(), m = t.length();
int l = lcm(n, m);
string s1 = "" , t1 = "" ;
for ( int i = 0; i < l / n; i++) {
s1 += s;
}
for ( int i = 0; i < l / m; i++) {
t1 += t;
}
if (s1 == t1)
cout << s1;
else
cout << -1;
}
int main()
{
string S = "baba" , T = "ba" ;
findSmallestString(S, T);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int lcm( int a, int b)
{
return (a / gcd(a, b)) * b;
}
static void findSmallestString(String s, String t)
{
int n = s.length(), m = t.length();
int l = lcm(n, m);
String s1 = "" , t1 = "" ;
for ( int i = 0 ; i < l / n; i++) {
s1 += s;
}
for ( int i = 0 ; i < l / m; i++) {
t1 += t;
}
if (s1.equals(t1)){
System.out.println(s1);
}
else {
System.out.println(- 1 );
}
}
public static void main(String[] args)
{
String S = "baba" , T = "ba" ;
findSmallestString(S, T);
}
}
|
Python3
def gcd(a, b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def lcm(a, b):
return (a / / gcd(a, b)) * b
def findSmallestString(s, t):
n, m = len (s), len (t)
l = lcm(n, m)
s1, t1 = " ", " "
for i in range (l / / n):
s1 + = s
for i in range (l / / m):
t1 + = t
if (s1 = = t1):
print (s1)
else :
print ( - 1 )
if __name__ = = '__main__' :
S, T = "baba" , "ba"
findSmallestString(S, T)
|
C#
using System;
public class GFG
{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int lcm( int a, int b)
{
return (a / gcd(a, b)) * b;
}
static void findSmallestString( string s, string t)
{
int n = s.Length, m = t.Length;
int l = lcm(n, m);
string s1 = "" , t1 = "" ;
for ( int i = 0; i < l / n; i++) {
s1 += s;
}
for ( int i = 0; i < l / m; i++) {
t1 += t;
}
if (s1 == t1)
Console.WriteLine(s1);
else
Console.WriteLine(-1);
}
public static void Main(String[] args)
{
string S = "baba" , T = "ba" ;
findSmallestString(S, T);
}
}
|
Javascript
<script>
function gcd(a,b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function lcm(a,b)
{
return (a / gcd(a, b)) * b;
}
function findSmallestString(s,t)
{
let n = s.length, m = t.length;
let l = lcm(n, m);
let s1 = "" , t1 = "" ;
for (let i = 0; i < l / n; i++) {
s1 += s;
}
for (let i = 0; i < l / m; i++) {
t1 += t;
}
if (s1 == (t1)){
document.write(s1+ "<br>" );
}
else {
document.write(-1+ "<br>" );
}
}
let S = "baba" , T = "ba" ;
findSmallestString(S, T);
</script>
|
Time Complexity: O(max(N, M))
Auxiliary Space: O(max(N, M))
Share your thoughts in the comments
Please Login to comment...