📌  相关文章
📜  生成字典上最小的字符串0、1 和 2,并允许相邻交换

📅  最后修改于: 2022-05-13 01:57:06.564000             🧑  作者: Mango

生成字典上最小的字符串0、1 和 2,并允许相邻交换

给定一个仅包含字符012的字符串str ,您可以交换任意两个相邻(连续)字符01或任意两个相邻(连续)字符12 。任务是通过使用这些交换任意次数来获得最小可能的(按字典顺序)字符串。
例子:

方法:您可以将所有1一起打印,因为1可以与其他字符交换,而02不能交换,因此所有02将遵循与原始字符串相同的顺序。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print the required string
void printString(string str, int n)
{
    // count number of 1s
    int ones = 0;
    for (int i = 0; i < n; i++)
        if (str[i] == '1')
            ones++;
 
    // To check if the all the 1s
    // have been used or not
    bool used = false;
 
    for (int i = 0; i < n; i++) {
        if (str[i] == '2' && !used) {
            used = 1;
 
            // Print all the 1s if any 2 is encountered
            for (int j = 0; j < ones; j++)
                cout << "1";
        }
 
        // If str[i] = 0 or str[i] = 2
        if (str[i] != '1')
            cout << str[i];
    }
 
    // If 1s are not printed yet
    if (!used)
        for (int j = 0; j < ones; j++)
            cout << "1";
}
 
// Driver code
int main()
{
    string str = "100210";
    int n = str.length();
    printString(str, n);
    return 0;
}


Java
// Java implementation of the approach
 
class GFG
{
 
// Function to print the required string
static void printString(char[] str, int n)
{
    // count number of 1s
    int ones = 0;
    for (int i = 0; i < n; i++)
        if (str[i] == '1')
            ones++;
 
    // To check if the all the 1s
    // have been used or not
    boolean used = false;
 
    for (int i = 0; i < n; i++)
    {
        if (str[i] == '2' && !used)
        {
            used = true;
 
            // Print all the 1s if any 2 is encountered
            for (int j = 0; j < ones; j++)
                System.out.print("1");
        }
 
        // If str[i] = 0 or str[i] = 2
        if (str[i] != '1')
            System.out.print(str[i]);
 
    }
 
    // If 1s are not printed yet
    if (!used)
        for (int j = 0; j < ones; j++)
            System.out.print("1");
}
 
// Driver code
public static void main(String[] args)
{
    String str = "100210";
    int n = str.length();
    printString(str.toCharArray(), n);
}
}
 
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 implementation of the approach
 
# Function to print the required string
def printString(Str1, n):
 
    # count number of 1s
    ones = 0
    for i in range(n):
        if (Str1[i] == '1'):
            ones += 1
 
    # To check if the all the 1s
    # have been used or not
    used = False
 
    for i in range(n):
        if (Str1[i] == '2' and used == False):
            used = 1
 
            # Print all the 1s if any 2 is encountered
            for j in range(ones):
                print("1", end = "")
 
        # If Str1[i] = 0 or Str1[i] = 2
        if (Str1[i] != '1'):
            print(Str1[i], end = "")
 
    # If 1s are not printed yet
    if (used == False):
        for j in range(ones):
            print("1", end = "")
 
# Driver code
Str1 = "100210"
n = len(Str1)
printString(Str1, n)
 
# This code is contributed
# by Mohit Kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
// Function to print the required string
static void printString(char[] str, int n)
{
    // count number of 1s
    int ones = 0;
    for (int i = 0; i < n; i++)
        if (str[i] == '1')
            ones++;
 
    // To check if the all the 1s
    // have been used or not
    bool used = false;
 
    for (int i = 0; i < n; i++)
    {
        if (str[i] == '2' && !used)
        {
            used = true;
 
            // Print all the 1s if any 2 is encountered
            for (int j = 0; j < ones; j++)
                Console.Write("1");
        }
 
        // If str[i] = 0 or str[i] = 2
        if (str[i] != '1')
            Console.Write(str[i]);
 
    }
 
    // If 1s are not printed yet
    if (!used)
        for (int j = 0; j < ones; j++)
            Console.Write("1");
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "100210";
    int n = str.Length;
    printString(str.ToCharArray(), n);
}
}
 
// This code has been contributed by 29AjayKumar


PHP


Javascript


输出:
001120