You are viewing a single comment's thread. Return to all comments →
my simple solution import java.util.*; class Solution{
public static void main(String []argh) { Scanner sc = new Scanner(System.in); Stack<Character> stack=new Stack<>(); while (sc.hasNext()) { String input=sc.next(); //Complete the code char[] c=input.toCharArray(); for(char a:c) { if(a=='['||a=='('||a=='{') { stack.push(a); continue; } if(a==']'&&!stack.empty()&&stack.peek()=='[') { stack.pop(); continue; } if(a==')'&&!stack.empty()&&stack.peek()=='(') { stack.pop(); continue; } if(a=='}'&&!stack.empty()&&stack.peek()=='{') { stack.pop(); continue; } if(a==']'||a==')'||a=='}') { stack.push(a); break; } } System.out.println(stack.isEmpty()); stack.clear(); } }
}
Seems like cookies are disabled on this browser, please enable them to open this website
Java Stack
You are viewing a single comment's thread. Return to all comments →
my simple solution import java.util.*; class Solution{
}