检查表达式 C# 中的平衡括号

希望你过得很好。我一直在努力处理这段代码。我认为这一定有效,但我无法准确地看到我出错的问题(我就像盲人一样)。
你能帮忙解决这个问题吗... 在此先感谢。两个字符串都返回 FALSE。但首先必须为真。

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class PatternFinder : MonoBehaviour
{
    public void Check()
    {
        String test_good = "()(){}{}{()}";//this must return TRUE
        String test_bad = "((({}{}))()";//this must return FALSE

        Debug.Log(checkBalanced(test_good));
        Debug.Log(checkBalanced(test_bad));
    }

    public static bool checkBalanced(String check)
    {
        Stack<char> stack = new Stack<char>();
        for (int i = 0; i < check.Length; i++)
        {
            char character = check[i];
            if (character == '[' || character == '{' || character == '(')
                stack.Push(character);
            else if (character == ']' || character == '}' || character == ')')
            {
                if (stack.Any())
                    return false;
                switch (character)
                {
                    case ']':
                        if (stack.Pop() != '[')
                            return false;
                        break;
                    case '}':
                        if (stack.Pop() != '{')
                            return false;
                        break;
                    case ')':
                        if (stack.Pop() != '(')
                            return false;
                        break;
                    default:
                        break;
                }
            }
        }
        if (stack.Any())
            return true;
        return false;
    }
}
stack overflow Check for balanced parentheses in an expression C#
原文答案

答案:

作者头像

您需要尝试此问题。它在所有情况下都能正常工作。

public string checkBalanced(string s)
 {
        string result =  "YES";
        Stack stack  = new Stack();

        for(int i = 0 ; i<s.Length;i++){

            if(s[i].ToString() == "{" 
               || s[i].ToString() == "(" 
               || s[i].ToString() == "["){

                stack.Push(s[i].ToString());
            }
            else if(stack.Count >0){

                if(s[i].ToString() == "}"){

                    if(stack.Peek().ToString() == "{"){
                        stack.Pop();
                    }else{
                        result = "NO";
                    }
                }else if(s[i].ToString() == "]"){

                    if(stack.Peek().ToString() == "["){
                        stack.Pop();
                    }else{
                        result = "NO";
                    }
                }else if(s[i].ToString() == ")"){

                    if(stack.Peek().ToString() == "("){

                        stack.Pop();
                    }else{
                        result = "NO";
                    }     
                }
                }else{
                    Console.WriteLine(count +  " " + s[i].ToString());
                    result = "NO";
                }
            }

        if(stack.Count >0){
            result = "NO";
        }

        return result;
    }