Accepted c# solution


  • 0
    A
     public bool IsValid(string s)
            {   
                if(s.Length < 2) return false;
                var i = 0;
                while(s.Length > 0 && i <s.Length)
                {
                    if (")]}".Contains(s[i]))
                    {
                        if (i == 0) return false;
                        char execptedOpening = ' ';
                        switch (s[i])
                        {
                            case ')':
                                execptedOpening = '(';
                                break;
                            case ']':
                                execptedOpening = '[';
                                break;
                            case '}':
                                execptedOpening = '{';
                                break;
                        }
                        if (s[i - 1] == execptedOpening)
                        {
                            s = s.Remove(i - 1, 2);
                            i=i-2;
                        }
                        else
                        {
                            return false;
                        }
                    
                    }
                    i++;
                }
                return s.Length == 0;
            }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.