# Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.

**Periodic Binary String** : A Binary string is called periodic if it can be written as repetition of a binary string of smaller or same length. For example, 101010 is a periodic binary string with period 10 as we can get the string by repeatedly appending 10 to itself. In general, the string **S** with period **P** means, S_{i} is equal to S_{i + P}.

Problem : Given an binary string **S**, the task is to find the **periodic** string with minimum possible period with the following additional conditions

1) The given string S should be a subsequence of the result

2) Length of the result should not be more than twice the length of input string.

**Examples:**

Input:S = “01”

Output:0101

Explanation:The output string has period as 2 and has given string as subsequence.

Input :S = “111”

Output :111

Explanation:The output string has period as 1.

Input:S = “110”

Output:101010

Explanation:The output string has period as 2 and has given string as subsequence. Please note that 110110 is not answer because the period length is more.

**Approach:**

The main idea depends on two possibility :

- If the string consists all 1s or all 0s, the answer is the given string
**S**itself having period as**1**. - If the string consists of dissimilar elements, find the string with period
**2**and having length as**twice**the length of given string**S**

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// periodic string with minimum period ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the periodic string ` `// with minimum period ` `void` `findPeriodicString(string S) ` `{ ` ` ` `int` `l = 2 * S.length(); ` ` ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i < S.length(); i++) { ` ` ` `if` `(S[i] == ` `'1'` `) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// Print the string S if it ` ` ` `// consists of similar elements ` ` ` `if` `(count == S.length() || count == 0) ` ` ` `cout << S << ` `"\n"` `; ` ` ` ` ` `// Find the required periodic ` ` ` `// string with period 2 ` ` ` `else` `{ ` ` ` `char` `arr[l]; ` ` ` `for` `(` `int` `i = 0; i < l; i += 2) { ` ` ` `arr[i] = ` `'1'` `; ` ` ` `arr[i + 1] = ` `'0'` `; ` ` ` `} ` ` ` ` ` `for` `(` `int` `i = 0; i < l; i++) ` ` ` `cout << arr[i]; ` ` ` `cout << ` `"\n"` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string S = ` `"1111001"` `; ` ` ` `findPeriodicString(S); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the ` `# periodic with minimum period ` ` ` `# Function to find the periodic string ` `# with minimum period ` `def` `findPeriodicString(S): ` ` ` `l ` `=` `2` `*` `len` `(S) ` ` ` ` ` `count ` `=` `0` ` ` `for` `i ` `in` `range` `(` `len` `(S)): ` ` ` `if` `(S[i] ` `=` `=` `'1'` `): ` ` ` `count ` `+` `=` `1` ` ` ` ` `# Print the S if it ` ` ` `# consists of similar elements ` ` ` `if` `(count ` `=` `=` `len` `(S) ` `or` `count ` `=` `=` `0` `): ` ` ` `print` `(S) ` ` ` ` ` `# Find the required periodic ` ` ` `# with period 2 ` ` ` `else` `: ` ` ` `arr ` `=` `[` `'0'` `]` `*` `l ` ` ` `for` `i ` `in` `range` `(` `0` `, l, ` `2` `): ` ` ` `arr[i] ` `=` `'1'` ` ` `arr[i ` `+` `1` `] ` `=` `'0'` ` ` ` ` `for` `i ` `in` `range` `(l): ` ` ` `print` `(arr[i],end` `=` `"") ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `S ` `=` `"1111001"` ` ` `findPeriodicString(S) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

10101010101010

**Time Complexity :**O(N)

## Recommended Posts:

- Minimum number of moves to make a binary array K periodic
- Longest subsequence of the form 0*1*0* in a binary string
- Minimum number of swaps required to make the string K periodic
- Minimum cost to partition the given binary string
- Minimum flips required to keep all 1s together in a Binary string
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum steps to convert one binary string to other only using negation
- Minimum number of operations required to sum to binary string S
- Minimum swaps required to convert one binary string to another
- Minimum steps to remove substring 010 from a binary string
- Minimum given operations required to convert a given binary string to all 1's
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Minimum jumps required to group all 1s together in a given Binary string
- Minimum number of replacements to make the binary string alternating | Set 2
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Minimum number of swaps to make two binary string equal
- Minimum flips required in a binary string such that all K-size substring contains 1
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum swaps required to make a binary string alternating
- Minimum flips required to form given binary string where every flip changes all bits to its right as well

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.