my easy java code


  • 0
    Z

    public class Solution {
    public boolean isValid(String s) {
    Stack<Character> res = new Stack<Character>();
    for(char c:s.toCharArray()){
    if(res.isEmpty() || c == '(' || c == '[' || c == '{')
    res.push(c);
    else if((c == ')' && res.peek() == '(') ||(c == ']' && res.peek() == '[')||(c == '}' && res.peek() == '{')){
    res.pop();
    }
    else
    return false;
    }
    return res.isEmpty() ?true:false;
    }
    }


Log in to reply
 

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