java


  • 0
    P

    public class Solution {
    public boolean isValid(String s) {
    char[] c = s.toCharArray();
    Stack<Character> stack = new Stack<Character>();
    for(int i = 0;i<c.length;i++)
    {
    if(c[i] == '('||c[i] == '{'||c[i] == '[')
    {
    stack.push(c[i]);
    }
    if(c[i] == ')'||c[i] == '}'||c[i] == ']')
    {
    if(stack.empty())
    {
    return false;
    }
    char chr = stack.pop();
    if(chr =='(')
    {
    chr = ')';
    }
    else if(chr == '{')
    {
    chr = '}';
    }
    else if(chr == '[')
    {
    chr = ']';
    }
    if(chr != c[i])
    {
    return false;
    }
    }
    }
    if(stack.empty())
    {
    return true;
    }
    return false;
    }
    }


Log in to reply
 

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