📌  相关文章
📜  检查给定的括号表达式是否平衡

📅  最后修改于: 2021-09-03 03:50:27             🧑  作者: Mango

给定一个长度为N的字符串str ,仅由 ‘ ( ‘ 和 ‘ ) ‘ 组成,任务是检查它是否平衡。
例子:

方法:

  • 声明一个Flag变量,表示表达式是否平衡。
  • 使用 true 初始化Flag变量,使用 0 初始化Count变量。
  • 遍历给定的表达式
    1. 如果我们遇到一个左括号( , 将计数增加 1
    2. 如果我们遇到右括号) ,则将计数减 1
    3. 如果 Count 在任何时候变为负数,则表示表达式不平衡,
      所以将 Flag 标记为 false 并中断循环。
  • 遍历表达式后,如果Count不等于0,
    这意味着表达式不平衡,因此将 Flag 标记为 false。
  • 最后,如果 Flag 为真,则表达式是平衡的,否则不平衡。

下面是上述方法的实现:

C
// C program of the above approach
#include  
#include 
  
// Function to check if
// parentheses are balanced
bool isBalanced(char exp[])
{
    // Initialising Variables
    bool flag = true;
    int count = 0;
  
    // Traversing the Expression
    for (int i = 0; exp[i] != '\0'; i++) {
  
        if (exp[i] == '(') {
            count++;
        }
        else {
            // It is a closing parenthesis
            count--;
        }
        if (count < 0) {
            // This means there are
            // more Closing parenthesis
            // than opening ones
            flag = false;
            break;
        }
    }
  
    // If count is not zero,
    // It means there are more
    // opening parenthesis
    if (count != 0) {
        flag = false;
    }
  
    return flag;
}
  
// Driver code
int main()
{
    char exp1[] = "((()))()()";
  
    if (isBalanced(exp1))
        printf("Balanced \n");
    else
        printf("Not Balanced \n");
  
    char exp2[] = "())((())";
  
    if (isBalanced(exp2))
        printf("Balanced \n");
    else
        printf("Not Balanced \n");
  
    return 0;
}


C++
// C++ program for the above approach.
  
#include  
using namespace std;
  
// Function to check
// if parentheses are balanced
bool isBalanced(string exp)
{
  
    // Initialising Variables
    bool flag = true;
    int count = 0;
  
    // Traversing the Expression
    for (int i = 0; i < exp.length(); i++) {
  
        if (exp[i] == '(') {
            count++;
        }
        else {
  
            // It is a closing parenthesis
            count--;
        }
        if (count < 0) {
  
            // This means there are
            // more Closing parenthesis
            // than opening ones
            flag = false;
            break;
        }
    }
  
    // If count is not zero,
    // It means there are
    // more opening parenthesis
    if (count != 0) {
        flag = false;
    }
  
    return flag;
}
  
// Driver code
int main()
{
    string exp1 = "((()))()()";
  
    if (isBalanced(exp1))
        cout << "Balanced \n";
    else
        cout << "Not Balanced \n";
  
    string exp2 = "())((())";
  
    if (isBalanced(exp2))
        cout << "Balanced \n";
    else
        cout << "Not Balanced \n";
  
    return 0;
}


Java
// Java program for the above approach. 
class GFG{
  
// Function to check 
// if parentheses are balanced 
public static boolean isBalanced(String exp) 
{
      
    // Initialising variables 
    boolean flag = true; 
    int count = 0; 
      
    // Traversing the expression 
    for(int i = 0; i < exp.length(); i++)
    { 
        if (exp.charAt(i) == '(') 
        { 
            count++; 
        } 
        else
        { 
              
            // It is a closing parenthesis 
            count--; 
        } 
        if (count < 0)
        { 
              
            // This means there are 
            // more Closing parenthesis 
            // than opening ones 
            flag = false; 
            break; 
        } 
    } 
      
    // If count is not zero, 
    // It means there are 
    // more opening parenthesis 
    if (count != 0) 
    { 
        flag = false; 
    }
    return flag; 
} 
  
// Driver code
public static void main(String[] args)
{
    String exp1 = "((()))()()"; 
      
    if (isBalanced(exp1)) 
        System.out.println("Balanced");
    else
        System.out.println("Not Balanced");
      
    String exp2 = "())((())"; 
      
    if (isBalanced(exp2)) 
        System.out.println("Balanced");
    else
        System.out.println("Not Balanced");
}
}
  
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above approach
  
# Function to check if 
# parenthesis are balanced
def isBalanced(exp):
  
    # Initialising Variables
    flag = True
    count = 0
  
    # Traversing the Expression
    for i in range(len(exp)):
        if (exp[i] == '('):
            count += 1
        else:
              
            # It is a closing parenthesis
            count -= 1
  
        if (count < 0):
  
            # This means there are 
            # more closing parenthesis 
            # than opening
            flag = False
            break
  
    # If count is not zero , 
    # it means there are more 
    # opening parenthesis
    if (count != 0):
        flag = False
  
    return flag
  
# Driver code
if __name__ == '__main__':
      
  
    exp1 = "((()))()()"
  
    if (isBalanced(exp1)):
        print("Balanced")
    else:
        print("Not Balanced")
  
    exp2 = "())((())"
  
    if (isBalanced(exp2)):
        print("Balanced")
    else:
        print("Not Balanced")
  
# This code is contributed by himanshu77


C#
// C# program for the above approach. 
using System;
  
class GFG{
  
// Function to check 
// if parentheses are balanced 
public static bool isBalanced(String exp) 
{
      
    // Initialising variables 
    bool flag = true; 
    int count = 0; 
      
    // Traversing the expression 
    for(int i = 0; i < exp.Length; i++)
    { 
        if (exp[i] == '(') 
        { 
            count++; 
        } 
        else
        { 
              
            // It is a closing parenthesis 
            count--; 
        } 
        if (count < 0)
        { 
              
            // This means there are 
            // more Closing parenthesis 
            // than opening ones 
            flag = false; 
            break; 
        } 
    } 
      
    // If count is not zero, 
    // It means there are 
    // more opening parenthesis 
    if (count != 0) 
    { 
        flag = false; 
    }
    return flag; 
} 
  
// Driver code
public static void Main(String[] args)
{
    String exp1 = "((()))()()"; 
      
    if (isBalanced(exp1)) 
        Console.WriteLine("Balanced");
    else
        Console.WriteLine("Not Balanced");
      
    String exp2 = "())((())"; 
      
    if (isBalanced(exp2)) 
        Console.WriteLine("Balanced");
    else
        Console.WriteLine("Not Balanced");
}
}
  
// This code is contributed by Amit Katiyar


输出:
Balanced 
Not Balanced

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live