Generate N sized binary string with prefix S and is lexicographically smallest possible Last Updated : 30 Jan, 2023 Comments Improve Suggest changes Like Article Like Report Given a binary string S, the task is to make a binary string of size N from the given string S (don't change the position of characters) following the below conditions: The prefix of the string is S.If is the lexicographically smallest possible.Absolute difference between the number of 1s and 0s is minimum. Examples: Input: S = "101", N = 8Output: 10100011Explanation: The prefix of output string is same as the string S. The absolute difference between number of 0s and 1s is 0.It is the lexicographically smallest possible which follows all the given condition. Input: S = "001", N = 7Output: 0010011 Approach: This problem can be solved by using the Greedy Approach based on the following idea: In the case of a binary string, a string starting with '0' is lexicographically smaller than the one starting with '1'.So, firstly make S the prefix and then find how many more 0s and 1s can be added. To make it lexicographically smallest, add all the 0s first and then the 1s. Follow the steps mentioned below to implement the approach. Count numbers of 1's and 0's and store them (say in count1 and count0 respectively).Find the difference between (say g) count0 and count1 and between N and length of S (say in a variable l).Calculate the size which we need to increment in string length to make string length = N and store it in l.Now add as many 0s (which will be (l-g)/2) such that the number of 0 becomes the same as N/2 and then add 1s for the remaining places. Below is the implementation of the above approach: C++ #include <iostream> using namespace std; // Function to build string string find_string(int n, string s) { // Declaring variable int count0 = 0, count1 = 0; // Check number of 1's and 0's for (int i = 0; i < s.length(); i++) { if (s[i] == '0') { count0++; } else { count1++; } } string sb = s; // Store difference in number of 1's // and 0's int g = count0 - count1; // l store the value that how much 0's // or 1's we need to add in string int l = n - s.length(); l -= g; // u store the count of // number of 0's we need to insert int u = l / 2; while (u > 0) { sb += '0'; u--; } if (l % 2 != 0) { sb += '0'; } while (sb.length() < n) { sb += '1'; } // Return result return sb; } // Driver code int main() { int N = 7; string S = "001"; // Function call cout << find_string(N, S); } // This code is contributed by phasing17 Java // Java program for above approach import java.io.*; import java.lang.*; class GFG { // Function to build string String find_string(int n, String s) { // Declaring variable int count0 = 0, count1 = 0; // Check number of 1's and 0's for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '0') { count0++; } else { count1++; } } String sb = s; // Store difference in number of 1's // and 0's int g = count0 - count1; // l store the value that how much 0's // or 1's we need to add in string int l = n - s.length(); l -= g; // u store the count of // number of 0's we need to insert int u = l / 2; while (u > 0) { sb += '0'; u--; } if (l % 2 != 0) { sb += '0'; } while (sb.length() < n) { sb += '1'; } // Return result return sb; } // Driver code public static void main(String[] args) { int N = 7; String S = "001"; GFG g = new GFG(); // Function call System.out.println(g.find_string(N, S)); } } Python3 # Python program for above approach # Function to build string def find_string(n, s): # Declaring variable count0 = 0 count1 = 0 # Check number of 1's and 0's for i in range(len(s)): if (s[i] == '0'): count0 += 1 else: count1 += 1 sb = s # Store difference in number of 1's # and 0's g = count0 - count1 # l store the value that how much 0's # or 1's we need to add in string l = n - len(s) l -= g # u store the count of # number of 0's we need to insert u = l // 2 while (u > 0): sb += '0' u -= 1 if (l % 2 != 0): sb += '0' while (len(sb) < n): sb += '1' # Return result return sb # Driver Code N = 7 S = "001" # Function call print(find_string(N, S)) # This code is contributed by shinjanpatra C# // C# code to implement the above approach using System; class GFG { // Function to build string string find_string(int n, string s) { // Declaring variable int count0 = 0, count1 = 0; // Check number of 1's and 0's for (int i = 0; i < s.Length; i++) { if (s[i] == '0') { count0++; } else { count1++; } } string sb = s; // Store difference in number of 1's // and 0's int g = count0 - count1; // l store the value that how much 0's // or 1's we need to add in string int l = n - s.Length; l -= g; // u store the count of // number of 0's we need to insert int u = l / 2; while (u > 0) { sb += '0'; u--; } if (l % 2 != 0) { sb += '0'; } while (sb.Length < n) { sb += '1'; } // Return result return sb; } // Driver code public static void Main() { int N = 7; string S = "001"; GFG g = new GFG(); // Function call Console.Write(g.find_string(N, S)); } } // This code is contributed by sonjoy_62. JavaScript <script> // Javascript program for above approach // Function to build string function find_string(n, s) { // Declaring variable let count0 = 0; let count1 = 0; // Check number of 1's and 0's for (let i = 0; i < s.length; i++) { if (s[i] == '0') { count0++; } else { count1++; } } let sb = s; // Store difference in number of 1's // and 0's let g = count0 - count1; // l store the value that how much 0's // or 1's we need to add in string let l = n - s.length; l -= g; // u store the count of // number of 0's we need to insert let u = Math.floor(l / 2); while (u > 0) { sb += '0'; u--; } if (l % 2 != 0) { sb += '0'; } while (sb.length < n) { sb += '1'; } // Return result return sb; } // Driver Code let N = 7; let S = "001"; // Function call document.write(find_string(N, S)); // This code is contributed by Samim Hossain Mondal. </script> Output0010011 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Generate N sized binary string with prefix S and is lexicographically smallest possible L lakshayr32 Follow Improve Article Tags : Strings Greedy DSA Practice Tags : GreedyStrings Similar Reads Lexicographically smallest string with given string as prefix Given an array arr[] consisting of N strings and a string S if size M, the task is to find the lexicographically smallest string consisting of the string S as the prefix. If there doesn't exist any string starting with prefix S then print "-1". Examples: Input: arr[] = {"apple", "appe", "apl", "aapl 6 min read Find Lexicographically smallest String with Prefix Reversal Given a string str of length N. You have to choose a non-zero integer K (K <= N), such that in one operation you can take the prefix of the string of length K and append the reverse of it to itself, the task is to find the lexicographically smallest string you can get. Examples: Input: str = "bvd 5 min read Lexicographically smallest string possible by using given operations Given a string S consisting of digits from 0 to 9 inclusive, the task is to form the lexicographically smallest string by performing an operation any number of times. In one operation you can choose any position i and delete the digit d at s[i] and insert min(d+1, 9) on any position (at the beginnin 7 min read Lexicographically smallest permutation of [1, N] based on given Binary string Given a binary string S of size (N - 1), the task is to find the lexicographically smallest permutation P of the first N natural numbers such that for every index i, if S[i] equals '0' then P[i + 1] must be greater than P[i] and if S[i] equals '1' then P[i + 1] must be less than P[i]. Examples: Inpu 6 min read Lexicographically smallest string formed by concatenating any prefix and its mirrored form Given a string str of N characters, the task is to find the lexicographically smallest string that can be formed by concatenating any prefix and its mirrored form. Examples: Input: str = "geeksforgeeks"Output: geeeegExplanation: The lexicographically smallest string can be formed with the prefix "ge 5 min read Find Mth lexicographically smallest Binary String with no two adjacent 1 Given two integers N and M, the task is to find the Mth lexicographically smallest binary string (have only characters 1 and 0) of length N where there cannot be two consecutive 1s. Examples: Input: N = 2, M = 3.Output: 10Explanation: The only strings that can be made of size 2 are ["00", "01", "10" 6 min read Lexicographically smallest subsequence possible by removing a character from given string Given a string S of length N, the task is to find the lexicographically smallest subsequence of length (N - 1), i.e. by removing a single character from the given string. Examples: Input: S = "geeksforgeeks"Output: "eeksforgeeks"Explanation: Lexicographically smallest subsequence possible is "eeksfo 8 min read Minimum size lexicographically smallest string which is not a substring of given string Given a string s, the task is to find the lexicographically smallest string of minimum characters that do not exist as a substring in S. Examples: Input: S = "aabacdefghijklmnopqrstuvwxyz"Output: adExplanation: All the single digit strings from [a-z] occur in the given string and in two character st 7 min read Lexicographically smallest string formed by removing at most one character Given a string s, the task is to find the lexicographically smallest string that can be formed by removing at most one character from the given string. Examples: Input: s = "abcda" Output: "abca"Explanation: One can remove 'd' to get "abca" which is the lexicographically smallest string possible. In 4 min read Find lexicographical smallest string by performing the given operations N times Given a string S of N characters, the task is to find the smallest lexicographical string after performing each of the following operations N times in any order: Remove the 1st character of S and insert it into a stack X.Remove the top of stack X and append it to the end of another string Y which is 8 min read Like