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:
- Store the Least Common Multiple of N and M in a variable, say L.
- Initialize two strings S1 and T1.
- Concatenate the string, S1 with string, S, (L/N) number of times.
- Concatenate the string, T1 with string, T, (L/M) number of times.
- If the strings S1 and T1 are equal, then print S1 as the result.
- 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 calculate// GCD of two numbersint gcd(int a, int b){ if (b == 0) return a; return gcd(b, a % b);}// Function to calculate// LCM of two numbersint lcm(int a, int b){ return (a / gcd(a, b)) * b;}// Function to find the smallest string// which is divisible by strings S and Tvoid 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 Codeint main(){ string S = "baba", T = "ba"; findSmallestString(S, T); return 0;} |
Java
// Java program for above approachimport 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 numbersdef gcd(a, b): if (b == 0): return a return gcd(b, a % b)# Function to calculate# LCM of two numbersdef lcm(a, b): return (a // gcd(a, b)) * b# Function to find the smallest string# which is divisible by strings S and Tdef 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 Codeif __name__ == '__main__': S, T = "baba", "ba" findSmallestString(S, T)# This code is contributed by mohit kumar 29. |
C#
// C# program for above approachusing 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. |
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.


