balanced parentheses in an expression using linked list code example

Example: balanced parentheses leetcode

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

Tags:

Misc Example