Find the shortest Binary string containing one or more occurrences of given strings Last Updated : 15 Sep, 2021 Comments Improve Suggest changes Like Article Like Report Given two Binary strings, S1 and S2, the task is to generate a new Binary strings (of least length possible) which can be stated as one or more occurrences of S1 as well as S2. If it is not possible to generate such a string, return -1 in output. Please note that the resultant string must not have incomplete strings S1 or S2. For example, "1111" can be the resultant string for "11" and "1111" as it is 1 occurrence of "1111" and can also be judged as two occurrences of "11". Examples: Input: S1 = "1010", S2 = "101010"Output: 101010101010Explanation: The resultant string is 3 occurrences of s1 and 2 occurrences of s2. Input: S1 = "000", S2 = "101"Output: -1Explanation: There is no way possible to construct such a string. Approach: If it is possible to make such a string, then it's length will be the LCM of lengths of strings S1 and S2. Because only then, it can be stated as a minimum multiple of string S1 and S2. Follow the steps below to solve the problem: Define a function repeat(int k, string S) and perform the following tasks:Initialize the string r as an empty string.Iterate over the range [0, K] and perform the following steps:Append the string S to the variable r.Return the string r as the answer.Initialize the variables x and y as the lengths of the strings S1 and S2.Initialize the variable gcd as the GCD of x and y.Call the function repeat(y/gcd, s1) to form the string S1 that many times and store that into the variable A.Call the function repeat(x/gcd, s2) to form the string S2 that many times and store that into the variable B.If A is equal to B, then print any one of them as the answer, else print "NO". Below is the implementation of the above approach. C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to form the resultant string string repeat(int k, string S) { string r = ""; while (k--) { r += S; } return r; } // Function to find if any such string // exists or not. If yes, find it void find(string s1, string s2) { int x = s1.size(), y = s2.size(); // GCD of x and y int gcd = __gcd(x, y); // Form the resultant strings string A = repeat(y / gcd, s1); string B = repeat(x / gcd, s2); // If both the strings are same, // then print the answer if (A == B) { cout << A; } else { cout << "-1"; } } // Driver Code int main() { // Initializing strings string s1 = "1010", s2 = "101010"; find(s1, s2); return 0; } Java // Java program for the above approach import java.io.*; class GFG { public static int GCD(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return GCD(a - b, b); return GCD(a, b - a); } public static String repeat(int k, String S) { String r = ""; while (k--!=0) { r += S; } return r; } // Function to find if any such string // exists or not. If yes, find it public static void find(String s1, String s2) { int x = s1.length(), y = s2.length(); // GCD of x and y int gcd = GCD(x, y); // Form the resultant strings String A = repeat(y / gcd, s1); String B = repeat(x / gcd, s2); // If both the strings are same, // then print the answer if (A.equals(B)) { System.out.println(A); } else { System.out.println("-1"); } } // Driver Code public static void main(String[] args) { // Initializing strings String s1 = "1010", s2 = "101010"; find(s1, s2); } } // This code is contributed by maddler. Python3 # Python program for the above approach import math # Function to form the resultant string def repeat(k, S): r = "" while (k): r += S k-=1 return r # Function to find if any such string # exists or not. If yes, find it def find(s1, s2): x = len(s1) y = len(s2) # GCD of x and y gcd = math.gcd(x, y) # Form the resultant strings A = repeat(y // gcd, s1) B = repeat(x // gcd, s2) # If both the strings are same, # then print answer if (A == B): print(A) else: print("-1") # Driver Code # Initializing strings s1 = "1010" s2 = "101010" find(s1, s2) # This code is contributed by shivanisinghss2110 C# // C# program for the above approach using System; class GFG{ public static int GCD(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return GCD(a - b, b); return GCD(a, b - a); } public static string repeat(int k, string S) { string r = ""; while (k--!=0) { r += S; } return r; } // Function to find if any such string // exists or not. If yes, find it public static void find(string s1, string s2) { int x = s1.Length, y = s2.Length; // GCD of x and y int gcd = GCD(x, y); // Form the resultant strings string A = repeat(y / gcd, s1); string B = repeat(x / gcd, s2); // If both the strings are same, // then print the answer if (A.Equals(B)) { Console.Write(A); } else { Console.Write("-1"); } } // Driver Code public static void Main() { // Initializing strings string s1 = "1010", s2 = "101010"; find(s1, s2); } } // This code is contributed by code_hunt. JavaScript <script> // Javascript program for the above approach function GCD(a, b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // Base case if (a == b) return a; // a is greater if (a > b) return GCD(a - b, b); return GCD(a, b - a); } // Function to form the resultant string function repeat(k, S) { var r = ""; while (k-- != 0) { r += S; } return r; } // Function to find if any such string // exists or not. If yes, find it function find(s1, s2) { var x = s1.length, y = s2.length; // GCD of x and y var gcd = GCD(x, y); // Form the resultant strings var A = repeat(y / gcd, s1); var B = repeat(x / gcd, s2); // If both the strings are same, // then print the answer if (A == B) { document.write(A); } else { document.write("-1"); } } // Driver Code // Initializing strings var s1 = "1010", s2 = "101010"; find(s1, s2); // This code is contributed by shivanisinghss2110 </script> Output101010101010 Time Complexity: O(m + n + log(max(m, n)))Auxiliary Space: O(n) (For storing the strings A & B) Comment More infoAdvertise with us Next Article Find the shortest Binary string containing one or more occurrences of given strings P parthmanchanda81 Follow Improve Article Tags : Misc Strings Pattern Searching DSA binary-string +1 More Practice Tags : MiscPattern SearchingStrings Similar Reads Count of substrings of a binary string containing K ones Given a binary string of length N and an integer K, we need to find out how many substrings of this string are exist which contains exactly K ones. Examples: Input : s = â10010â K = 1 Output : 9 The 9 substrings containing one 1 are, â1â, â10â, â100â, â001â, â01â, â1â, â10â, â0010â and â010âRecommen 7 min read Find Binary String of size at most 3N containing at least 2 given strings of size 2N as subsequences Given three binary strings a, b, and c each having 2*N characters each, the task is to find a string having almost 3*N characters such that at least two of the given three strings occur as its one of the subsequence. Examples: Input: a = "00", b = "11", c = "01"Output: "010"Explanation: The strings 11 min read Count of substrings that start and end with 1 in given Binary String Given a binary string, count the number of substrings that start and end with 1. Examples: Input: "00100101"Output: 3Explanation: three substrings are "1001", "100101" and "101" Input: "1001"Output: 1Explanation: one substring "1001" Recommended PracticeCount SubstringsTry It!Count of substrings tha 12 min read Find the longest Substring of a given String S Given a string S of length, N. Find the maximum length of any substring of S such that, the bitwise OR of all the characters of the substring is equal to the bitwise OR of the remaining characters of the string. If no such substring exists, print -1. Examples: Input: S = "2347"Output: 3?Explanation: 10 min read Count occurrences of substring X before every occurrence of substring Y in a given string Given three strings S, X, and Y consisting of N, A, and B characters respectively, the task is to find the number of occurrences of the substring X before every occurrence of the substring Y in the given string S. Examples: Input S = âabcdefdefabcâ, X = âdefâ, Y = âabcâOutput: 0 2Explanation:First o 6 min read Count ways to generate Binary String not containing "0100" Substring Given the number N, count the number of ways to create a binary string (the string that contains characters as zero or one) of size N such that it does not contain "0100" as a substring. A substring is a contiguous sequence of characters within a string. Examples: Input: N = 4Output: 15Explanation: 15+ min read Minimize removal of substring of 0s to remove all occurrences of 0s from a circular Binary String Given circular binary string S of size N, the task is to count the minimum number of consecutive 0s required to be removed such that the string contains only 1s. A circular string is a string whose first and last characters are considered to be adjacent to each other. Examples: Input: S = "11010001" 6 min read Find all K length subarrays containing only 1s in given Binary String Given a binary string str[], the task is to find all possible K length subarrays containing only 1s and print their starting and ending index. Examples: Input: str = "0101000", K=1Output: 1 13 3Explanation: Substrings at positions 1 and 3 are the substrings with value 1. Input: str = "11111001", K=3 6 min read Count of substrings of a Binary string containing only 1s Given a binary string of length N, we need to find out how many substrings of this string contain only 1s. Examples: Input: S = "0110111"Output: 9Explanation:There are 9 substring with only 1's characters. "1" comes 5 times. "11" comes 3 times. "111" comes 1 time. Input: S = "000"Output: 0 The_Appro 6 min read Smallest string obtained by removing all occurrences of 01 and 11 from Binary String Given a binary string S , the task is to find the smallest string possible by removing all occurrences of substrings "01" and "11". After removal of any substring, concatenate the remaining parts of the string. Examples: Input: S = "0010110"Output:Length = 1 String = 0Explanation: String can be tran 7 min read Like