Dada la string str que consta de los caracteres del conjunto {‘o’, ‘n’, ‘e’, ’z’, ‘r’} , la tarea es encontrar el mayor número binario posible que se puede formar reorganizando los caracteres de la string dada. Tenga en cuenta que la string formará al menos un número válido.
Ejemplos:
Entrada: str = “roenenzooe”
Salida: 110
“oneonezero” es la string requerida.Entrada: str = “cerocerocerouno”
Salida: 1000
Enfoque: Cree un mapa y almacene la frecuencia de ‘z’ y ‘n’ en él porque estos son los únicos caracteres que solo aparecerán en 0 o 1 y no en ambos. El número de unos en la string será igual a la frecuencia de ‘n’ y el número de ceros en la string será igual a la frecuencia de ‘z’ en el mapa. Ahora, para encontrar el número más grande, imprime todos los unos seguidos de todos los ceros.
A continuación se muestra la implementación del enfoque anterior:
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>
110
Complejidad de tiempo: O(N)
Publicación traducida automáticamente
Artículo escrito por jrolofmeister y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA