Skip to content
Related Articles

Related Articles

Smallest string divisible by two given strings
  • Difficulty Level : Medium
  • Last Updated : 10 Mar, 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 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 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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate
// GCD of two numbers
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Function to calculate
// LCM of two numbers
int lcm(int a, int b)
{
    return (a / gcd(a, b)) * b;
}
 
// Function to find the smallest string
// which is divisible by strings S and T
void findSmallestString(string s, string t)
{
    // Store the length of both strings
    int n = s.length(), m = t.length();
 
    // Store LCM of n and m
    int l = lcm(n, m);
 
    // Temporary strings to store
    // concatenated strings
    string s1 = "", t1 = "";
 
    // Concatenate s1 (l / n) times
    for (int i = 0; i < l / n; i++) {
        s1 += s;
    }
 
    // Concatenate t1 (l / m) times
    for (int i = 0; i < l / m; i++) {
        t1 += t;
    }
 
    // If s1 and t1 are equal
    if (s1 == t1)
        cout << s1;
 
    // Otherwise, print -1
    else
        cout << -1;
}
 
// Driver Code
int main()
{
    string S = "baba", T = "ba";
    findSmallestString(S, T);
 
    return 0;
}

Java




// Java program for above approach
import java.io.*;
 
class GFG
{
 
  // Function to calculate
  // GCD of two numbers
  static int gcd(int a, int b)
  {
    if (b == 0)
      return a;
    return gcd(b, a % b);
  }
 
  // Function to calculate
  // LCM of two numbers
  static int lcm(int a, int b)
  {
    return (a / gcd(a, b)) * b;
  }
 
  // Function to find the smallest string
  // which is divisible by strings S and T
  static void findSmallestString(String s, String t)
  {
    // Store the length of both strings
    int n = s.length(), m = t.length();
 
    // Store LCM of n and m
    int l = lcm(n, m);
 
    // Temporary strings to store
    // concatenated strings
    String s1 = "", t1 = "";
 
    // Concatenate s1 (l / n) times
    for (int i = 0; i < l / n; i++) {
      s1 += s;
    }
 
    // Concatenate t1 (l / m) times
    for (int i = 0; i < l / m; i++) {
      t1 += t;
    }
 
    // If s1 and t1 are equal
    if (s1.equals(t1)){
      System.out.println(s1);
    }
 
    // Otherwise, print -1
    else{
      System.out.println(-1);
    }
  }
 
  // Driver code
  public static void main(String[] args)
  {
 
    String S = "baba", T = "ba";
    findSmallestString(S, T);
  }
}
 
// This code is contributed by susmitakundugoaldanga.

Python3




# Python3 program for the above approach
 
# Function to calculate
# GCD of two numbers
def gcd(a, b):
    if (b == 0):
        return a
    return gcd(b, a % b)
 
# Function to calculate
# LCM of two numbers
def lcm(a, b):
    return (a // gcd(a, b)) * b
 
# Function to find the smallest string
# which is divisible by strings S and T
def findSmallestString(s, t):
    # Store the length of both strings
    n, m = len(s), len(t)
 
    # Store LCM of n and m
    l = lcm(n, m)
 
    # Temporary strings to store
    # concatenated strings
    s1, t1 = "", ""
 
    # Concatenate s1 (l / n) times
    for i in range(l//n):
        s1 += s
 
    # Concatenate t1 (l / m) times
    for i in range(l//m):
        t1 += t
 
    # If s1 and t1 are equal
    if (s1 == t1):
        print(s1)
 
    # Otherwise, pr-1
    else:
        print(-1)
 
# Driver Code
if __name__ == '__main__':
    S, T = "baba", "ba"
    findSmallestString(S, T)
 
# This code is contributed by mohit kumar 29.

C#




// C# program for above approach
using System;
 
public class GFG
{
 
  // Function to calculate
  // GCD of two numbers
  static int gcd(int a, int b)
  {
    if (b == 0)
      return a;
    return gcd(b, a % b);
  }
 
  // Function to calculate
  // LCM of two numbers
  static int lcm(int a, int b)
  {
    return (a / gcd(a, b)) * b;
  }
 
  // Function to find the smallest string
  // which is divisible by strings S and T
  static void findSmallestString(string s, string t)
  {
    // Store the length of both strings
    int n = s.Length, m = t.Length;
 
    // Store LCM of n and m
    int l = lcm(n, m);
 
    // Temporary strings to store
    // concatenated strings
    string s1 = "", t1 = "";
 
    // Concatenate s1 (l / n) times
    for (int i = 0; i < l / n; i++) {
      s1 += s;
    }
 
    // Concatenate t1 (l / m) times
    for (int i = 0; i < l / m; i++) {
      t1 += t;
    }
 
    // If s1 and t1 are equal
    if (s1 == t1)
      Console.WriteLine(s1);
 
    // Otherwise, print -1
    else
      Console.WriteLine(-1);
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    string S = "baba", T = "ba";
    findSmallestString(S, T);
  }
}
 
// This code is contributed by sanjoy_62.

 
 

Output: 
baba

 

 

Time Complexity: O(max(N, M))
Auxiliary Space: O(max(N, M))

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :