📜  将不平衡的括号序列转换为平衡的序列

📅  最后修改于: 2021-06-26 11:29:54             🧑  作者: Mango

给定的“(”“)”的非平衡支架序列,将其转换成一个平衡的序列通过在字符串的末尾“(”在字符串的开头和“)”加入的最小数目。

例子:

方法:

  • 让我们假设,每当遇到开括号时,深度增加一,而当开括号时,深度减少一。
  • 在minDep中找到最大负深度,并在开头添加该数量的‘(’
  • 然后循环新序列以找到字符串末尾所需的‘)数量并添加它们。
  • 最后,返回字符串。

下面是该方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return balancedBrackets string
string balancedBrackets(string str)
{
    // Initializing dep to 0
    int dep = 0;
 
    // Stores maximum negative depth
    int minDep = 0;
 
    for (int i = 0; i < str.length(); i++)
    {
        if (str[i] == '(')
            dep++;
        else
            dep--;
 
        // if dep is less than minDep
        if (minDep > dep)
            minDep = dep;
    }
 
    // if minDep is less than 0 then there
    // is need to add '(' at the front
    if (minDep < 0)
    {
        for (int i = 0; i < abs(minDep); i++)
            str = '(' + str;
    }
 
    // Reinitializing to check the updated string
    dep = 0;
 
    for (int i = 0; i < str.length(); i++)
    {
        if (str[i] == '(')
            dep++;
        else
            dep--;
    }
 
    // if dep is not 0 then there
    // is need to add ')' at the back
    if (dep != 0)
    {
        for (int i = 0; i < dep; i++)
            str = str + ')';
    }
    return str;
}
 
// Driver code
int main()
{
    string str = ")))()";
    cout << balancedBrackets(str);
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG {
 
    // Function to return balancedBrackets string
    static String balancedBrackets(String str)
    {
        // Initializing dep to 0
        int dep = 0;
 
        // Stores maximum negative depth
        int minDep = 0;
 
        for (int i = 0; i < str.length(); i++)
        {
            if (str.charAt(i) == '(')
                dep++;
            else
                dep--;
 
            // if dep is less than minDep
            if (minDep > dep)
                minDep = dep;
        }
 
        // if minDep is less than 0 then there
        // is need to add '(' at the front
        if (minDep < 0)
        {
            for (int i = 0; i < Math.abs(minDep); i++)
                str = '(' + str;
        }
 
        // Reinitializing to check the updated string
        dep = 0;
 
        for (int i = 0; i < str.length(); i++)
        {
            if (str.charAt(i) == '(')
                dep++;
            else
                dep--;
        }
 
        // if dep is not 0 then there
        // is need to add ')' at the back
        if (dep != 0)
        {
            for (int i = 0; i < dep; i++)
                str = str + ')';
        }
        return str;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str = ")))()";
        System.out.println(balancedBrackets(str));
    }
}
 
// This code is contributed by ihritik


Python3
# Python3 implementation of the approach
 
# Function to return balancedBrackets String
 
 
def balancedBrackets(Str):
 
    # Initializing dep to 0
    dep = 0
 
    # Stores maximum negative depth
    minDep = 0
 
    for i in Str:
        if (i == '('):
            dep += 1
        else:
            dep -= 1
 
        # if dep is less than minDep
        if (minDep > dep):
            minDep = dep
 
    # if minDep is less than 0 then there
    # is need to add '(' at the front
    if (minDep < 0):
        for i in range(abs(minDep)):
            Str = '(' + Str
 
    # Reinitializing to check the updated String
    dep = 0
 
    for i in Str:
        if (i == '('):
            dep += 1
        else:
            dep -= 1
 
    # if dep is not 0 then there
    # is need to add ')' at the back
    if (dep != 0):
        for i in range(dep):
            Str = Str + ')'
 
    return Str
 
 
# Driver code
Str = ")))()"
print(balancedBrackets(Str))
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
 
class GFG {
 
    // Function to return balancedBrackets string
    static string balancedBrackets(string str)
    {
        // Initializing dep to 0
        int dep = 0;
 
        // Stores maximum negative depth
        int minDep = 0;
 
        for (int i = 0; i < str.Length; i++)
        {
            if (str[i] == '(')
                dep++;
            else
                dep--;
 
            // if dep is less than minDep
            if (minDep > dep)
                minDep = dep;
        }
 
        // if minDep is less than 0 then there
        // is need to add '(' at the front
        if (minDep < 0)
        {
            for (int i = 0; i < Math.Abs(minDep); i++)
                str = '(' + str;
        }
 
        // Reinitializing to check the updated string
        dep = 0;
 
        for (int i = 0; i < str.Length; i++)
        {
            if (str[i] == '(')
                dep++;
            else
                dep--;
        }
 
        // if dep is not 0 then there
        // is need to add ')' at the back
        if (dep != 0)
        {
            for (int i = 0; i < dep; i++)
                str = str + ')';
        }
        return str;
    }
 
    // Driver code
    public static void Main()
    {
        String str = ")))()";
        Console.WriteLine(balancedBrackets(str));
    }
}
 
// This code is contributed by ihritik


Javascript


输出
((()))()

时间复杂度: O(N)
辅助空间: O(N)

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