📌  相关文章
📜  从给定的字符串找到最大可能的二进制数

📅  最后修改于: 2021-05-25 09:13:21             🧑  作者: Mango

给定一个字符串str ,该字符串str由集合{‘o’,’n’,’e’,’z’,’r’}中的字符组成,任务是查找可以通过重新排列而形成的最大二进制数给定字符串中的字符。请注意,该字符串将至少形成一个有效数字。

例子:

方法:创建一个映射并在其中存储“ z”“ n”的频率,因为这是仅有的字符,它们只会以0或1出现,而不会同时出现在两个字符中。字符串的1的数量将等于“ n”的频率,而字符串的零的数量将等于映射中的“ z”的频率。现在要找到最大的数字,请打印所有数字,然后打印所有零。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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

时间复杂度: O(N)