# Lexicographically smallest Binary String formed using infinite swaps

• Last Updated : 11 Apr, 2022

Given a binary string s of length N, the task is to find the lexicographically smallest string using infinite number of swaps between 0’s and 1’s.

Examples:

Input: s = “1001001”
Output: 0000111
Explanation: Lexicographically smallest string of 1001001 is only 0000111

Input: s = “0001”
Output: 0001
Explanation: Lexicographically smallest string of 0001 is only 0001

Input: s = “1”
Output: 1
Explanation: Lexicographically smallest string of 1 is only 1

Naive Approach: The most basic approach to solve this problem is based on below idea:

Since we are allowed to do infinite swaps between 0s and 1s. Therefore while swapping, we will get one such combination where all 0s will come before all 1s.

As by definition, lexicographically smallest binary string must have all 0s before all 1s, therefore, the above such combination will yield the required output.

Now inorder to find such a combination, we can do swapping of 0s and 1s one by one, till we get the required combination where all 0s are before all 1s.

Time Complexity: O(N!), to find all such combinations of given binary string
Auxiliary Space: O(1)

Approach 2: The above approach can be optimised by avoiding the need to find all combinations, based on following observation:

Since we need to find a combination where all 0s come before all 1s, we can sort the given binary string instead, to get the required lexicographically smallest binary string.

Time Complexity: O(N*log(N))
Auxiliary Space: O(1)

Efficient approach: The above approach can be further optimised by avoiding the need to sort the given binary string altogether, as per below idea:

Instead of sorting given binary string, we can simply find the count of set and unset bits (0s and 1s) and form a new binary with the same count where all 0s come before all 1s.

Based on the above idea, follow the steps below to implement this approach:

• Count the number of 0s and 1s in given binary string.
• Create a new empty string
• Insert 0s in the empty string equal to the count of 0s in original string.
• Then append 1s in the new string equal to the count of 1s in original string.
• Return the new string as the lexicographically smallest binary string.

Below is the implementation of the above approach:

## C++

 `// C++ code to implement the above approach` `#include ``using` `namespace` `std;` `// Function to find the count of 0s``int` `countOfXs(string s, ``char` `x)``{``    ``int` `n = s.length();``    ``int` `no_of_x = 0;` `    ``for` `(``int` `i = 0; i < n; i++) {``        ``// if character is 0``        ``// then increase the count of 0's``        ``if` `(s[i] == x)``            ``no_of_x++;``    ``}` `    ``return` `no_of_x;``}` `// Function to find the lexicographically``// smallest string using infinite swaps``string lexSmallestBinaryString(string s)``{` `    ``// Variables to count no of 0 and no of 1``    ``int` `no_of_0 = countOfXs(s, ``'0'``);``    ``int` `no_of_1 = countOfXs(s, ``'1'``);` `    ``// Create new string to store``    ``// the required string``    ``s = ``""``;` `    ``// Put all 0's first in resultant string``    ``for` `(``int` `i = 0; i < no_of_0; i++) {``        ``// Make character equal to 0``        ``s += ``'0'``;``    ``}` `    ``// Append all 1's in resultant string``    ``for` `(``int` `i = 0; i < no_of_1; i++) {``        ``// Make character equal to 1``        ``s += ``'1'``;``    ``}` `    ``// Return the resultant string``    ``return` `s;``}` `// Driver code``int` `main()``{``    ``string s = ``"1111000011"``;` `    ``cout << lexSmallestBinaryString(s);` `    ``return` `0;``}`

## Java

 `// Java code to implement the approach``class` `GFG``{` `  ``// Function to find the count of 0s``  ``static` `int` `countOfXs(String s, ``char` `x)``  ``{``    ``int` `n = s.length();``    ``int` `no_of_x = ``0``;` `    ``for` `(``int` `i = ``0``; i < n; i++)``    ``{` `      ``// if character is 0``      ``// then increase the count of 0's``      ``if` `(s.charAt(i) == x)``        ``no_of_x++;``    ``}` `    ``return` `no_of_x;``  ``}` `  ``// Function to find the lexicographically``  ``// smallest string using infinite swaps``  ``static` `String lexSmallestBinaryString(String s)``  ``{` `    ``// Variables to count no of 0 and no of 1``    ``int` `no_of_0 = countOfXs(s, ``'0'``);``    ``int` `no_of_1 = countOfXs(s, ``'1'``);` `    ``// Create new string to store``    ``// the required string``    ``s = ``""``;` `    ``// Put all 0's first in resultant string``    ``for` `(``int` `i = ``0``; i < no_of_0; i++)``    ``{` `      ``// Make character equal to 0``      ``s += ``'0'``;``    ``}` `    ``// Append all 1's in resultant string``    ``for` `(``int` `i = ``0``; i < no_of_1; i++)``    ``{` `      ``// Make character equal to 1``      ``s += ``'1'``;``    ``}` `    ``// Return the resultant string``    ``return` `s;``  ``}``  ``public` `static` `void` `main(String[] args)``  ``{` `    ``String s = ``"1111000011"``;` `    ``System.out.println(lexSmallestBinaryString(s));``  ``}``}` `// This code is contributed by phasing17.`

## Python3

 `# Python3 code for the above approach` `# Function to find the count of 0s``def` `countOfXs(s, x):``    ``n ``=` `len``(s)``    ``no_of_x ``=` `0` `    ``for` `i ``in` `range``(n):``      ` `        ``# if character is 0``        ``# hen increase the count of 0's``        ``if` `s[i] ``=``=` `x:``            ``no_of_x ``+``=` `1``    ` `    ``return` `no_of_x` `# Function to find the lexicographically``# smallest string using infinite swaps``def` `lexSmallestBinaryString(s):``  ` `    ``# Variables to count no of 0 and no of 1``    ``no_of_0 ``=` `countOfXs(s, ``'0'``)``    ``no_of_1 ``=` `countOfXs(s, ``"1"``)``    ` `    ``# Create new string to store``    ``# the required string``    ``s ``=` `""``    ` `    ``# Put all 0's first in resultant string``    ``for` `i ``in` `range``(no_of_0):``      ` `        ``# Make character equal to 0``        ``s ``+``=` `"0"``        ` `    ``# Append all 1's in resultant string``    ``for` `i ``in` `range``(no_of_1):``      ` `        ``# Make character equal to 1``        ``s ``+``=` `"1"``        ` `    ``# Return the resultant string``    ``return` `s` `# Driver code``s ``=` `"1111000011"`` ` `# function call``print``(lexSmallestBinaryString(s))` `# This code is contributed by phasing17`

## C#

 `// C# code to implement the above approach``using` `System;``class` `GFG {` `    ``// Function to find the count of 0s``    ``static` `int` `countOfXs(``string` `s, ``char` `x)``    ``{``        ``int` `n = s.Length;``        ``int` `no_of_x = 0;` `        ``for` `(``int` `i = 0; i < n; i++) {``            ``// if character is 0``            ``// then increase the count of 0's``            ``if` `(s[i] == x)``                ``no_of_x++;``        ``}` `        ``return` `no_of_x;``    ``}` `    ``// Function to find the lexicographically``    ``// smallest string using infinite swaps``    ``static` `string` `lexSmallestBinaryString(``string` `s)``    ``{` `        ``// Variables to count no of 0 and no of 1``        ``int` `no_of_0 = countOfXs(s, ``'0'``);``        ``int` `no_of_1 = countOfXs(s, ``'1'``);` `        ``// Create new string to store``        ``// the required string``        ``s = ``""``;` `        ``// Put all 0's first in resultant string``        ``for` `(``int` `i = 0; i < no_of_0; i++) {``            ``// Make character equal to 0``            ``s += ``'0'``;``        ``}` `        ``// Append all 1's in resultant string``        ``for` `(``int` `i = 0; i < no_of_1; i++) {``            ``// Make character equal to 1``            ``s += ``'1'``;``        ``}` `        ``// Return the resultant string``        ``return` `s;``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``string` `s = ``"1111000011"``;` `        ``Console.Write(lexSmallestBinaryString(s));``    ``}``}` `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`0000111111`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up