Find the maximum possible Binary Number from given string | Java implementation of the approach ; Function to return maximum number that can be formed from the string ; To store the frequency of ' z ' and ' n ' in the given string ; Number of zeroes ; Number of ones ; To store the required number ; Add all the ones ; Add all the zeroes ; Driver Code
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 ) ) ; } }