📜  识别并标记表达式中不匹配的括号

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

识别并标记表达式中不匹配的括号

给定一个表达式,在其中找到并标记匹配和不匹配的括号。我们需要将所有平衡的左括号替换为 0,平衡的右括号替换为 1,所有不平衡的替换为 -1。
例子:

Input : ((a) 
Output : -10a1

Input : (a))
Output : 0a1-1

Input  : (((abc))((d)))))
Output : 000abc1100d111-1-1

这个想法是基于堆栈的。我们从字符串的开头到结尾运行一个循环,对于每个'(',我们将它推入堆栈。如果堆栈为空,并且遇到右括号')',我们将在该索引处替换-1字符串。否则,我们将所有左括号“(”替换为 0,将右括号替换为 1。然后从堆栈中弹出。

C++
// CPP program to mark balanced and unbalanced
// parenthesis.
#include 
using namespace std;
 
void identifyParenthesis(string a)
{
    stack st;
 
    // run the loop upto end of the string
    for (int i = 0; i < a.length(); i++) {
 
        // if a[i] is opening bracket then push
        // into stack
        if (a[i] == '(')
            st.push(i);
         
        // if a[i] is closing bracket ')'
        else if (a[i] == ')') {
 
            // If this closing bracket is unmatched
            if (st.empty())
                a.replace(i, 1, "-1");
             
            else {
 
                // replace all opening brackets with 0
                // and closing brackets with 1
                a.replace(i, 1, "1");
                a.replace(st.top(), 1, "0");
                st.pop();
            }
        }
    }
 
    // if stack is not empty then pop out all
    // elements from it and replace -1 at that
    // index of the string
    while (!st.empty()) {
        a.replace(st.top(), 1, "-1");
        st.pop();
    }
 
    // print final string
    cout << a << endl;
}
 
// Driver code
int main()
{
    string str = "(a))";
    identifyParenthesis(str);
    return 0;
}


Java
// Java program to mark balanced and
// unbalanced parenthesis.
import java.util.*;
 
class GFG
{
static void identifyParenthesis(StringBuffer a)
{
    Stack st = new Stack();
 
    // run the loop upto end of the string
    for (int i = 0; i < a.length(); i++)
    {
 
        // if a[i] is opening bracket then push
        // into stack
        if (a.charAt(i) == '(')
            st.push(i);
         
        // if a[i] is closing bracket ')'
        else if (a.charAt(i) == ')')
        {
 
            // If this closing bracket is unmatched
            if (st.empty())
                a.replace(i, i + 1, "-1");
             
            else
            {
 
                // replace all opening brackets with 0
                // and closing brackets with 1
                a.replace(i, i + 1, "1");
                a.replace(st.peek(), st.peek() + 1, "0");
                st.pop();
            }
        }
    }
 
    // if stack is not empty then pop out all
    // elements from it and replace -1 at that
    // index of the string
    while (!st.empty())
    {
        a.replace(st.peek(), 1, "-1");
        st.pop();
    }
 
    // print final string
    System.out.println(a);
}
 
// Driver code
public static void main(String[] args)
{
    StringBuffer str = new StringBuffer("(a))");
    identifyParenthesis(str);
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program to
# mark balanced and
# unbalanced parenthesis.
def identifyParenthesis(a):
    st = []
 
    # run the loop upto
    # end of the string
    for i in range (len(a)):
 
        # if a[i] is opening
        # bracket then push
        # into stack
        if (a[i] == '('):
            st.append(a[i])
         
        # if a[i] is closing bracket ')'
        elif (a[i] == ')'):
 
            # If this closing bracket
            # is unmatched
            if (len(st) == 0):
                a = a.replace(a[i], "-1", 1)
             
            else:
 
                # replace all opening brackets with 0
                # and closing brackets with 1
                a = a.replace(a[i], "1", 1)
                a = a.replace(st[-1], "0", 1)
                st.pop()
           
    # if stack is not empty
    # then pop out all
    # elements from it and
    # replace -1 at that
    # index of the string
    while (len(st) != 0):
        a = a.replace(st[-1], 1, "-1");
        st.pop()
 
    # print final string
    print(a)
 
# Driver code
if __name__ == "__main__":
 
    st = "(a))"
    identifyParenthesis(st)
     
# This code is contributed by Chitranayal


C#
// C# program to mark balanced and
// unbalanced parenthesis.
using System;
using System.Collections.Generic;
class GFG {
     
    static void identifyParenthesis(string a)
    {
        Stack st = new Stack();
  
        // run the loop upto end of the string
        for (int i = 0; i < a.Length; i++)
        {
  
            // if a[i] is opening bracket then push
            // into stack
            if (a[i] == '(')
                st.Push(i);
  
            // if a[i] is closing bracket ')'
            else if (a[i] == ')')
            {
  
                // If this closing bracket is unmatched
                if (st.Count == 0)
                {
                    a = a.Substring(0, i) + "-1" + a.Substring(i + 1);
                }
                else
                {
  
                    // replace all opening brackets with 0
                    // and closing brackets with 1
                    a = a.Substring(0, i) + "1" + a.Substring(i + 1);
                    a = a.Substring(0, st.Peek()) + "0" + a.Substring(st.Peek() + 1);
                    st.Pop();
                }
            }
        }
  
        // if stack is not empty then pop out all
        // elements from it and replace -1 at that
        // index of the string
        while (st.Count > 0)
        {
            a = a.Substring(0, st.Peek()) + "-1" + a.Substring(st.Peek() + 1);
            st.Pop();
        }
  
        // print final string
        Console.Write(new string(a));
    }
     
  static void Main() {
    string str = "(a))";
    identifyParenthesis(str);
  }
}
 
// This code is contributed by divyesh072019.


Javascript


输出:

0a1-1