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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
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[2] = { 0 };
  
    for (int i = 0; i < n; i++) {
        if (str[i] == 'z') {
  
            // Number of zeroes
            freq[0]++;
        }
        else if (str[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
int main()
{
    string str = "roenenzooe";
    int n = str.length();
  
    cout << maxNumber(str, n);
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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[2];
  
        for (int i = 0; i < n; i++)
        {
            if (str[i] == 'z'
            {
      
                // Number of zeroes
                freq[0]++;
            }
            else if (str[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 str = "roenenzooe";
        int n = str.Length;
        Console.Write(maxNumber(str, n)); 
    
}
  
// This code is contributed by Sanjit Prasad

chevron_right


Output:

110

Time Complexity: O(N)



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.



Improved By : AnkitRai01, Sanjit_Prasad