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 stringInput: 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++
// 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; } |
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[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 |
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.
Recommended Posts:
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Maximum contiguous 1 possible in a binary string after k rotations
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Maximum splits in binary string such that each substring is divisible by given odd number
- Maximum decimal equivalent possible among all connected components of a Binary Valued Graph
- Find a number such that maximum in array is minimum possible after XOR
- Find all possible ways to Split the given string into Primes
- Check if it is possible to rearrange a binary string with alternate 0s and 1s
- Generate a string with maximum possible alphabets with odd frequencies
- Check if binary representations of 0 to N are present as substrings in given binary string
- Count of Binary Strings possible as per given conditions
- Maximum number of set bits count in a K-size substring of a Binary String
- Sum of decimal equivalent of all possible pairs of Binary representation of a Number
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Largest even number possible by using one swap operation in given number
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- String Range Queries to find the number of subsets equal to a given String
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.

