# Find the maximum possible Binary Number from given string

Given a string str consisting of the characters from the set {‘o’, ‘n’, ‘e’, ‘z’, ‘r’}, the task is to to find the largest possible binary number that can be formed by rearranging the characters of the given string. Note that the string will form at least a valid number.

Examples:

Input: str = “roenenzooe”
Output: 110
“oneonezero” is the required string

Input: str = “zerozerozeroone”
Output: 1000

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Create a map and store the frequency of ‘z’ and ‘n’ in it because these are the only characters which will only appear either in 0 or 1 and not both. The number of ones in the string will be equal to the frequency of ‘n’ and the number of zeroes in the string will be equal to the frequency of ‘z’ in the map. Now to find the largest number, print all the ones followed by all the zeroes.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return maximum number ` `// that can be formed from the string ` `string maxNumber(string str, ``int` `n) ` `{ ` `    ``// To store the frequency of 'z' and 'n' ` `    ``// in the given string ` `    ``int` `freq = { 0 }; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``if` `(str[i] == ``'z'``) { ` ` `  `            ``// Number of zeroes ` `            ``freq++; ` `        ``} ` `        ``else` `if` `(str[i] == ``'n'``) { ` ` `  `            ``// Number of ones ` `            ``freq++; ` `        ``} ` `    ``} ` ` `  `    ``// To store the requried number ` `    ``string num = ``""``; ` ` `  `    ``// Add all the ones ` `    ``for` `(``int` `i = 0; i < freq; i++) ` `        ``num += ``'1'``; ` ` `  `    ``// Add all the zeroes ` `    ``for` `(``int` `i = 0; i < freq; i++) ` `        ``num += ``'0'``; ` ` `  `    ``return` `num; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string str = ``"roenenzooe"``; ` `    ``int` `n = str.length(); ` ` `  `    ``cout << maxNumber(str, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to return maximum number ` `    ``// that can be formed from the string ` `    ``static` `String maxNumber(String str, ``int` `n) ` `    ``{ ` ` `  `        ``// To store the frequency of 'z' and 'n' ` `        ``// in the given string ` `        ``int``[] freq = ``new` `int``[``2``]; ` ` `  `        ``for` `(``int` `i = ``0``; i < n; i++) ` `        ``{ ` `            ``if` `(str.charAt(i) == ``'z'``) ` ` `  `                ``// Number of zeroes ` `                ``freq[``0``]++; ` `                 `  `            ``else` `if` `(str.charAt(i) == ``'n'``) ` ` `  `                ``// Number of ones ` `                ``freq[``1``]++; ` `        ``} ` ` `  `        ``// To store the requried number ` `        ``String num = ``""``; ` ` `  `        ``// Add all the ones ` `        ``for` `(``int` `i = ``0``; i < freq[``1``]; i++) ` `            ``num += ``'1'``; ` ` `  `        ``// Add all the zeroes ` `        ``for` `(``int` `i = ``0``; i < freq[``0``]; i++) ` `            ``num += ``'0'``; ` ` `  `        ``return` `num; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``String str = ``"roenenzooe"``; ` `        ``int` `n = str.length(); ` ` `  `        ``System.out.println(maxNumber(str, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by ` `// sanjeev2552 `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function to return maximum number  ` `# that can be formed from the string  ` `def` `maxNumber(string , n) : ` `     `  `    ``# To store the frequency of 'z' and 'n'  ` `    ``# in the given string  ` `    ``freq ``=` `[``0``, ``0``] ` `     `  `    ``for` `i ``in` `range``(n) : ` `        ``if` `(string[i] ``=``=` `'z'``) : ` ` `  `            ``# Number of zeroes  ` `            ``freq[``0``] ``+``=` `1``;  ` ` `  `        ``elif` `(string[i] ``=``=` `'n'``) : ` ` `  `            ``# Number of ones  ` `            ``freq[``1``] ``+``=` `1``;  ` ` `  `    ``# To store the requried number  ` `    ``num ``=` `"";  ` ` `  `    ``# Add all the ones  ` `    ``for` `i ``in` `range``(freq[``1``]) : ` `        ``num ``+``=` `'1'``;  ` ` `  `    ``# Add all the zeroes  ` `    ``for` `i ``in` `range``(freq[``0``]) : ` `        ``num ``+``=` `'0'``;  ` ` `  `    ``return` `num;  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``string ``=` `"roenenzooe"``;  ` `    ``n ``=` `len``(string);  ` ` `  `    ``print``(maxNumber(string, n));  ` ` `  `# This code is contributed by AnkitRai01 `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{  ` `     `  `    ``// Function to return maximum number ` `    ``// that can be formed from the string ` `    ``static` `string` `maxNumber(``string` `str, ``int` `n) ` `    ``{ ` `        ``// To store the frequency of 'z' and 'n' ` `        ``// in the given string ` `        ``int` `[] freq = ``new` `int``; ` ` `  `        ``for` `(``int` `i = 0; i < n; i++) ` `        ``{ ` `            ``if` `(str[i] == ``'z'``)  ` `            ``{ ` `     `  `                ``// Number of zeroes ` `                ``freq++; ` `            ``} ` `            ``else` `if` `(str[i] == ``'n'``)  ` `            ``{ ` `     `  `                ``// Number of ones ` `                ``freq++; ` `            ``} ` `        ``} ` `     `  `        ``// To store the requried number ` `        ``string` `num = ``""``; ` `     `  `        ``// Add all the ones ` `        ``for` `(``int` `i = 0; i < freq; i++) ` `            ``num += ``'1'``; ` `     `  `        ``// Add all the zeroes ` `        ``for` `(``int` `i = 0; i < freq; i++) ` `            ``num += ``'0'``; ` `     `  `        ``return` `num; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``string` `str = ``"roenenzooe"``; ` `        ``int` `n = str.Length; ` `        ``Console.Write(maxNumber(str, n));  ` `    ``}  ` `} ` ` `  `// This code is contributed by Sanjit Prasad `

Output:

```110
```

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.