📜  使用给定的括号打印平衡的括号表达式

📅  最后修改于: 2021-06-25 23:34:05             🧑  作者: Mango

给定四个整数abcd ,表示四种类型的括号的数量。

  1. “((
  2. “()”
  3. “)(“
  4. “))”

任务是使用所有给定的括号打印任何平衡的括号表达式。如果我们不能形成一个平衡的括号表达式,则输出-1 。如果有多个答案,请打印任何一个。
例子:

方法:首先检查是否可以使用给定数量的括号形成平衡括号表达式。如果type1括号的数量等于type4括号的数量等于任意type3括号的数量,或者如果只有type 2括号,则我们可以形成表达式。因此,合并条件将是:

如果满足上述条件,可以按照以下步骤打印平衡括号表达式:

  • 打印1型括号的数量。
  • 打印3型括号的数量。
  • 打印4型括号的数量。
  • 打印2型括号的数量。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print balanced bracket
// expression if it is possible
void printBalancedExpression(int a, int b, int c, int d)
{
 
    // If the condition is met
    if ((a == d && a) || (a == 0 && c == 0 && d == 0)) {
 
        // Print brackets of type-1
        for (int i = 1; i <= a; i++)
            cout << "((";
 
        // Print brackets of type-3
        for (int i = 1; i <= c; i++)
            cout << ")(";
 
        // Print brackets of type-4
        for (int i = 1; i <= d; i++)
            cout << "))";
 
        // Print brackets of type-2
        for (int i = 1; i <= b; i++)
            cout << "()";
    }
 
    // If the condition is not met
    else
        cout << -1;
}
 
// Driver code
int main()
{
    int a = 3, b = 1, c = 4, d = 3;
    printBalancedExpression(a, b, c, d);
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG
{
     
    // Function to print balanced bracket
    // expression if it is possible
    static void printBalancedExpression(int a,
                            int b, int c, int d)
    {
     
        // If the condition is met
        if (((a == d) && (a != 0)) ||
        ((a == 0) && (c == 0) && (d == 0)))
        {
     
            // Print brackets of type-1
            for (int i = 1; i <= a; i++)
                System.out.print("((");
     
            // Print brackets of type-3
            for (int i = 1; i <= c; i++)
                System.out.print(")(");
     
            // Print brackets of type-4
            for (int i = 1; i <= d; i++)
                System.out.print("))");
     
            // Print brackets of type-2
            for (int i = 1; i <= b; i++)
                System.out.print("()");
        }
     
        // If the condition is not met
        else
            System.out.print(-1);
    }
     
    // Driver code
    public static void main(String args[])
    {
        int a = 3, b = 1, c = 4, d = 3;
        printBalancedExpression(a, b, c, d);
    }
}
 
// This code is contributed by Ryuga


Python3
# Python3 implementation of the approach
 
# Function to print balanced bracket
# expression if it is possible
def printBalancedExpression(a, b, c, d):
 
    # If the condition is met
    if ((a == d and a) or
        (a == 0 and c == 0 and d == 0)):
 
        # Print brackets of type-1
        for i in range(1, a + 1):
            print("((", end = "")
 
        # Print brackets of type-3
        for i in range(1, c + 1):
            print(")(", end = "")
 
        # Print brackets of type-4
        for i in range(1, d + 1):
            print("))", end = "")
 
        # Print brackets of type-2
        for i in range(1, b + 1):
            print("()", end = "")
     
    # If the condition is not met
    else:
        print("-1")
 
# Driver code
if __name__ == "__main__":
 
    a, b, c, d = 3, 1, 4, 3
    printBalancedExpression(a, b, c, d)
 
# This code is contributed by Rituraj Jain


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to print balanced bracket
// expression if it is possible
static void printBalancedExpression(int a,
                        int b, int c, int d)
{
 
    // If the condition is met
    if (((a == d) && (a != 0)) ||
       ((a == 0) && (c == 0) && (d == 0)))
    {
 
        // Print brackets of type-1
        for (int i = 1; i <= a; i++)
            Console.Write("((");
 
        // Print brackets of type-3
        for (int i = 1; i <= c; i++)
            Console.Write(")(");
 
        // Print brackets of type-4
        for (int i = 1; i <= d; i++)
            Console.Write("))");
 
        // Print brackets of type-2
        for (int i = 1; i <= b; i++)
            Console.Write("()");
    }
 
    // If the condition is not met
    else
        Console.Write(-1);
}
 
// Driver code
public static void Main()
{
    int a = 3, b = 1, c = 4, d = 3;
    printBalancedExpression(a, b, c, d);
}
}
 
// This code is contributed by Akanksha Rai


PHP


Javascript


输出:
(((((()()()()())))))()

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。