stack: Valid Parentheses. I have compiler error in line 6. I couldn’t correct the error.

public class Solution {
public bool IsValid(string s) {

    Stack<Character> stack = new Stack<>();
    
    for(char ch ; s.toCharArray()) {
        
        if (ch == '(' || ch == '[' || ch == '{')
            stack.push();
        
        if (ch == ')' || ch == ']' || ch == '}')
            
            if (stack.empty()) return false;
        
        var top = stack.pop();
        
        if(
            (ch == '(' && top != ')') ||
            (ch == '[' && top != ']') ||
            (ch == '{' && top != '}')
        ) return false;
    }
    return stack.empty();
}

}

Comments (0)