C# using stack


  • 0
    L

    public class Solution {
    public bool IsValid(string s) {
    Stack<char> stack = new Stack<char>();
    foreach (char c in s.ToCharArray())
    {
    if (c == '(')
    stack.Push(')');
    else if (c == '{')
    stack.Push('}');
    else if (c == '[')
    stack.Push(']');
    else if (stack.Count()==0 || stack.Pop() != c)
    return false;
    }
    if (stack.Count() == 0)
    return true;
    else return false;
    }
    }


Log in to reply
 

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