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++
// 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.
JavaScript
<script>
// Javascript program for above approach
// Function to calculate
// GCD of two numbers
function gcd(a,b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to calculate
// LCM of two numbers
function lcm(a,b)
{
return (a / gcd(a, b)) * b;
}
// Function to find the smallest string
// which is divisible by strings S and T
function findSmallestString(s,t)
{
// Store the length of both strings
let n = s.length, m = t.length;
// Store LCM of n and m
let l = lcm(n, m);
// Temporary strings to store
// concatenated strings
let s1 = "", t1 = "";
// Concatenate s1 (l / n) times
for (let i = 0; i < l / n; i++) {
s1 += s;
}
// Concatenate t1 (l / m) times
for (let i = 0; i < l / m; i++) {
t1 += t;
}
// If s1 and t1 are equal
if (s1 == (t1)){
document.write(s1+"<br>");
}
// Otherwise, print -1
else{
document.write(-1+"<br>");
}
}
// Driver code
let S = "baba", T = "ba";
findSmallestString(S, T);
// This code is contributed by unknown2108
</script>
Time Complexity: O(max(N, M))
Auxiliary Space: O(max(N, M))
Similar Reads
Number of sub-strings in a given binary string divisible by 2 Given binary string str of length N, the task is to find the count of substrings of str which are divisible by 2. Leading zeros in a substring are allowed. Examples: Input: str = "101" Output: 2 "0" and "10" are the only substrings which are divisible by 2. Input: str = "10010" Output: 10 Naive appr
4 min read
Prefix of a given String that are divisible by K Given a 0-indexed based string str of length n consisting of digits, and a positive integer K, the task is to find the prefixes of a String that are exactly Divisible by K. Store the prefix that is divisible by K, in a vector of String and print them. String word str consists of only digits from 0 t
5 min read
Number of substrings divisible by 6 in a string of integers Given a string consisting of integers 0 to 9. The task is to count the number of substrings which when convert into integer are divisible by 6. Substring does not contain leading zeroes. Examples: Input : s = "606". Output : 5 Substrings "6", "0", "6", "60", "606" are divisible by 6. Input : s = "48
9 min read
Largest sub-string of a binary string divisible by 2 Given binary string str of length N, the task is to find the longest sub-string divisible by 2. If no such sub-string exists then print -1. Examples: Input: str = "11100011" Output: 111000 Largest sub-string divisible by 2 is "111000".Input: str = "1111" Output: -1 There is no sub-string of the give
3 min read
Maximum splits in binary string such that each substring is divisible by given odd number Given binary string str, the task is to calculate the maximum possible splits possible to make each substring divisible by a given odd number K.Examples: Input: str = "110111001", K = 9 Output: 2 Explanation: The two possible substrings are "11011" and "1001". The equivalent decimal values are 27 an
5 min read
Partition a number into two divisible parts Given a number (as string) and two integers a and b, divide the string in two non-empty parts such that the first part is divisible by a and the second part is divisible by b. If the string can not be divided into two non-empty parts, output "NO", else print "YES" with the two parts. Examples: Input
15+ min read