Find the maximum possible Binary Number from given string
Last Updated :
12 Oct, 2022
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++
// 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 required 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;
}
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 required 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 required 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[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 required 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
JavaScript
<script>
// Javascript implementation of the approach
// Function to return maximum number
// that can be formed from the string
function maxNumber(str, n)
{
// To store the frequency of 'z' and 'n'
// in the given string
var freq = Array(2).fill(0);
for (var 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 required number
var num = "";
// Add all the ones
for (var i = 0; i < freq[1]; i++)
num += '1';
// Add all the zeroes
for (var i = 0; i < freq[0]; i++)
num += '0';
return num;
}
// Driver code
var str = "roenenzooe";
var n = str.length;
document.write( maxNumber(str, n));
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Remove one bit from a binary number to get maximum value Given a binary number, the task is to remove exactly one bit from it such that, after it's removal, the resultant binary number is greatest from all the options.Examples: Input: 110 Output: 11 As 110 = 6 in decimal, the option is to remove either 0 or 1. So the possible combinations are 10, 11 The m
4 min read
Maximum number in Binary tree of binary values Given a binary tree consisting of nodes, each containing a binary value of either 0 or 1, the task is to find the maximum decimal number that can be formed by traversing from the root to a leaf node. The maximum number is achieved by concatenating the binary values along the path from the root to a
6 min read
Maximum number of set bits count in a K-size substring of a Binary String Given a binary string S of size N and an integer K. The task is to find the maximum number of set bit appears in a substring of size K. Examples: Input: S = "100111010", K = 3 Output: 3 Explanation: The substring "111" contains 3 set bits. Input:S = "0000000", K = 4 Output: 0 Explanation: S doesn't
10 min read
Minimum number of operations required to maximize the Binary String Given a binary string S, the task is to find the minimum number of swaps required to be performed to maximize the value represented by S. Examples: Input: S = "1010001" Output: 1 Explanation: Swapping S[2] and S[7], modifies the string to 1110000, thus, maximizing the number that can be generated fr
7 min read
Minimize the maximum of 0s left or 1s deleted from Binary String You are given a binary string S of size N consisting of characters 0 and/or 1. You may remove several characters from the beginning or the end of the string. The task is to find the cost of removal where the cost is the maximum of the following two values: The number of characters 0 left in the stri
8 min read
Count of Binary Strings possible as per given conditions Given two integers N and M, where N denotes the count of '0' and M denotes the count of '1', and an integer K, the task is to find the maximum number of binary strings that can be generated of the following two types: A string can consist of K '0's and a single '1'.A string can consist of K '1's and
4 min read