We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Java
- Data Structures
- Java Stack
- Discussions
Java Stack
Java Stack
+ 0 comments import java.util.*; public class Solution01 { public static void main(String[] args) { Compare compare = new Compare(); Scanner sc = new Scanner(System.in); while(sc.hasNext()){ System.out.println(compare.check(sc.next())); } sc.close(); } } class Compare { static boolean openClose(char open, char close) { if (open == '(' && close == ')') return true; if (open == '{' && close == '}') return true; if (open == '[' && close == ']') return true; return false; } public boolean check(String input) { Stack<Character> stack = new Stack<>(); for (int i = 0; i < input.length(); i++) { char c = input.charAt(i); if (c == '(' || c == '{' || c == '[') { stack.push(c); } else if (c == ')' || c == '}' || c == ']') { if (stack.isEmpty()) { return false; } char lastOpened = stack.pop(); if (!openClose(lastOpened, c)) { return false; } } } return stack.isEmpty(); } }
+ 0 comments my java solution:
import java.util.*; class Solution{
public static void main(String []argh) { Scanner sc = new Scanner(System.in); Stack<Character> myStack = new Stack<Character>(); while (sc.hasNext()) { String input=sc.next(); while(myStack.size() != 0){ myStack.pop(); } for(int i = 0; i < input.length(); i++){ char ch = input.charAt(i); if (myStack.size() != 0 && ((ch == ')' && myStack.peek() == '(') || (ch == '}' && myStack.peek() == '{') || (ch == ']' && myStack.peek() == '['))) { myStack.pop(); } else { myStack.push(ch); } } if(myStack.size() == 0){ System.out.println("true"); } else { System.out.println("false"); } } }
}
+ 0 comments import java.util.*; class Solution{ public static boolean check(String input){ Stack<Character> s = new Stack<Character>(); for(char c: input.toCharArray()){ if(c == '{'){ s.push('}'); } else if(c == '['){ s.push(']'); } else if(c == '('){ s.push(')'); } else if(s.isEmpty() || s.pop() != c){ return false; } } return s.isEmpty(); } public static void main(String []argh) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String input=sc.next(); //Complete the code System.out.println(check(input)); } } }
+ 1 comment import java.util.*; class Solution{
public static void main(String []argh) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String input=sc.next(); //Complete the code Stack<Character> stack = new Stack<>(); char[] ch = input.toCharArray(); for (int i = 0; i < ch.length; i++) { if (!stack.isEmpty() && ((ch[i] == ')' && stack.peek() == '(') || (ch[i] == ']' && stack.peek() == '[') || (ch[i] == '}' && stack.peek() == '{')) ) { stack.pop(); } else { stack.push(ch[i]); } } System.out.println(stack.isEmpty() ? true : false); } sc.close(); }
}
+ 0 comments private final static String openBracket = "("; private final static String closeBracket = ")"; private final static String openSquareBracket = "["; private final static String closeSquareBracket = "]"; private final static String openParentheses = "{"; private final static String closeParentheses = "}"; public static void main(String[] argh) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String input = sc.next(); boolean isBalance = isBalance(input); System.out.println(isBalance); } } private static boolean isBalance(String input) { String[] s = input.split(""); Stack<String> stack = new Stack<>(); for (String symbol : s) { if (symbol.equals(openParentheses) || symbol.equals(openSquareBracket) || symbol.equals(openBracket)) { stack.push(symbol); } else if (stack.empty()){ return false; } else if (symbol.equals(closeParentheses)) { if (!Objects.equals(stack.pop(), openParentheses)) return false; } else if (symbol.equals(closeBracket)) { if (!Objects.equals(stack.pop(), openBracket)) return false; } else if (symbol.equals(closeSquareBracket)) { if (!Objects.equals(stack.pop(), openSquareBracket)) return false; } } return stack.empty(); }
Load more conversations
Sort 540 Discussions, By:
Please Login in order to post a comment