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++
// 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; } |
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)
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 difference of zeros and ones in binary string
- Maximum contiguous 1 possible in a binary string after k rotations
- Arrange a binary string to get maximum value within a range of indices
- Maximum Consecutive Zeroes in Concatenated Binary String
- Maximum difference of zeros and ones in binary string | Set 2 (O(n) time)
- Maximum length of consecutive 1's in a binary string in Python using Map function
- Maximum number of splits of a binary number
- Remove one bit from a binary number to get maximum value
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Find the first maximum length even word from a string
- String Range Queries to find the number of subsets equal to a given String
- Maximum number of consecutive 1's in binary representation of all the array elements
- Find maximum depth of nested parenthesis in a string
- Find the winner of a game where scores are given as a binary 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.



