Dados cuatro enteros a , b , c y d que significa el número de cuatro tipos de paréntesis.
- “((“
- “()”
- “)(“
- “))”
La tarea es imprimir cualquier expresión de paréntesis balanceada usando todos los paréntesis dados. Si no podemos formar una expresión de paréntesis equilibrada, imprima -1 . En caso de múltiples respuestas, imprima cualquiera.
Ejemplos:
Entrada: a = 3, b = 1, c = 4, d = 3
Salida: ((((()()()()())))))()
Entrada: a = 3, b = 1 , c = 4, d = 8
Salida: -1
No es posible una expresión de paréntesis equilibrada con los paréntesis dados.
Enfoque: primero verifique si la expresión de paréntesis balanceada se puede formar con el número dado de paréntesis. Podemos formar la expresión si el número de corchetes de tipo 1 es igual al número de corchetes de tipo 4 con cualquier número de corchetes de tipo 3 o si solo hay corchetes de tipo 2. Por lo tanto, la condición de combinación será:
(a == d && a) || (a == 0 && c == 0 && d == 0)
Se pueden seguir los siguientes pasos para imprimir la expresión de soporte equilibrado si se cumple la condición anterior:
- Imprime el número de corchetes de tipo 1.
- Escriba el número de corchetes de tipo 3.
- Escriba el número de corchetes tipo 4.
- Escriba el número de corchetes de tipo 2.
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 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
<?php //PHP implementation of the approach // Function to print balanced bracket // expression if it is possible function printBalancedExpression($a, $b, $c, $d) { // If the condition is met if (($a == $d && $a) || ($a == 0 && $c == 0 && $d == 0)) { // Print brackets of type-1 for ($i = 1; $i <= $a; $i++) echo "(("; // Print brackets of type-3 for ($i = 1; $i <= $c; $i++) echo ")("; // Print brackets of type-4 for ($i = 1; $i <= $d; $i++) echo "))"; // Print brackets of type-2 for ($i = 1; $i <= $b; $i++) echo "()"; } // If the condition is not met else echo -1; } // Driver code $a = 3; $b = 1; $c = 4; $d = 3; printBalancedExpression($a, $b, $c, $d); // This code is contributed by jit_t ?>
Javascript
<script> // Javascript implementation of the above approach // Function to print balanced bracket // expression if it is possible function printBalancedExpression(a , b , c , d) { // If the condition is met if (((a == d) && (a != 0)) || ((a == 0) && (c == 0) && (d == 0))) { // Print brackets of type-1 for (i = 1; i <= a; i++) document.write("(("); // Print brackets of type-3 for (i = 1; i <= c; i++) document.write(")("); // Print brackets of type-4 for (i = 1; i <= d; i++) document.write("))"); // Print brackets of type-2 for (i = 1; i <= b; i++) document.write("()"); } // If the condition is not met else document.write(-1); } // Driver code var a = 3, b = 1, c = 4, d = 3; printBalancedExpression(a, b, c, d); // This code contributed by umadevi9616 </script>
(((((()()()()())))))()
Complejidad de tiempo: O (max (a, b, c, d)), ya que estamos usando bucle para atravesar tiempos a, b, c y d.
Espacio auxiliar: O(1), ya que no estamos utilizando ningún espacio adicional.