Given string str consisting of the characters from the set {‘o’, ‘n’, ‘e’, ‘z’, ‘r’}, the task is 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 that 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++
#include <bits/stdc++.h>
using namespace std;
string maxNumber(string str, int n)
{
int freq[2] = { 0 };
for ( int i = 0; i < n; i++) {
if (str[i] == 'z' ) {
freq[0]++;
}
else if (str[i] == 'n' ) {
freq[1]++;
}
}
string num = "" ;
for ( int i = 0; i < freq[1]; i++)
num += '1' ;
for ( int i = 0; i < freq[0]; i++)
num += '0' ;
return num;
}
int main()
{
string str = "roenenzooe" ;
int n = str.length();
cout << maxNumber(str, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static String maxNumber(String str, int n)
{
int [] freq = new int [ 2 ];
for ( int i = 0 ; i < n; i++)
{
if (str.charAt(i) == 'z' )
freq[ 0 ]++;
else if (str.charAt(i) == 'n' )
freq[ 1 ]++;
}
String num = "" ;
for ( int i = 0 ; i < freq[ 1 ]; i++)
num += '1' ;
for ( int i = 0 ; i < freq[ 0 ]; i++)
num += '0' ;
return num;
}
public static void main(String[] args)
{
String str = "roenenzooe" ;
int n = str.length();
System.out.println(maxNumber(str, n));
}
}
|
Python3
def maxNumber(string , n) :
freq = [ 0 , 0 ]
for i in range (n) :
if (string[i] = = 'z' ) :
freq[ 0 ] + = 1 ;
elif (string[i] = = 'n' ) :
freq[ 1 ] + = 1 ;
num = "";
for i in range (freq[ 1 ]) :
num + = '1' ;
for i in range (freq[ 0 ]) :
num + = '0' ;
return num;
if __name__ = = "__main__" :
string = "roenenzooe" ;
n = len (string);
print (maxNumber(string, n));
|
C#
using System;
class GFG
{
static string maxNumber( string str, int n)
{
int [] freq = new int [2];
for ( int i = 0; i < n; i++)
{
if (str[i] == 'z' )
{
freq[0]++;
}
else if (str[i] == 'n' )
{
freq[1]++;
}
}
string num = "" ;
for ( int i = 0; i < freq[1]; i++)
num += '1' ;
for ( int i = 0; i < freq[0]; i++)
num += '0' ;
return num;
}
public static void Main()
{
string str = "roenenzooe" ;
int n = str.Length;
Console.Write(maxNumber(str, n));
}
}
|
Javascript
<script>
function maxNumber(str, n)
{
var freq = Array(2).fill(0);
for ( var i = 0; i < n; i++) {
if (str[i] == 'z' ) {
freq[0]++;
}
else if (str[i] == 'n' ) {
freq[1]++;
}
}
var num = "" ;
for ( var i = 0; i < freq[1]; i++)
num += '1' ;
for ( var i = 0; i < freq[0]; i++)
num += '0' ;
return num;
}
var str = "roenenzooe" ;
var n = str.length;
document.write( maxNumber(str, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)