HackerRank Java Stack problem solution YASH PAL, 31 July 2024 In this HackerRank java Stack problem in java programming language A string containing only parentheses is balanced if the following is true: 1. if it is an empty string 2. if A and B are correct, AB is correct, 3. if A is correct, (A) and {A} and [A] are also correct. Given a string, determine if it is balanced or not. HackerRank Java Stack problem solution. import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static boolean isBalanced(String s) { Stack<Character> stack = new Stack<Character>(); for (int i=0; i<s.length();++i){ if (s.charAt(i) == '(') stack.push('('); else if (s.charAt(i) == '{') stack.push('{'); else if (s.charAt(i) == '[') stack.push('['); else if (s.charAt(i) == ')') { if (stack.isEmpty()) return false; if (stack.pop() != '(') return false; } else if (s.charAt(i) == '}') { if (stack.isEmpty()) return false; if (stack.pop() != '{') return false; } else if (s.charAt(i) == ']') { if (stack.isEmpty()) return false; if (stack.pop() != '[') return false; } } return stack.isEmpty(); } public static void main(String[] args) { Stack<Character> stack = new Stack<Character>(); Scanner sc = new Scanner(System.in); String line; while (sc.hasNextLine()){ line = sc.nextLine(); if (isBalanced(line)) System.out.println("true"); else System.out.println("false"); } } } Second solution import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { private final static Map<String, String> BRACKETS = createBracketsMap(); private final static Set<String> CLOSING = new HashSet<String>(Arrays.asList("]", "}", ")")); public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()) { String data = in.next().trim(); System.out.println(isBalanced(data)); } } private static Map<String, String> createBracketsMap() { Map<String, String> map = new HashMap<String, String>(); map.put("{", "}"); map.put("[", "]"); map.put("(", ")"); return map; } public static boolean isBalanced(String data) { if (data.equals("")) return true; Deque<String> stack = new ArrayDeque<String>(); for (int i = 0; i < data.length(); i++){ String c = data.substring(i,i+1); if (CLOSING.contains(c)) { if (stack.isEmpty()) return false; String op = stack.pop(); if (!BRACKETS.get(op).equals(c)) return false; } else { stack.push(c); } } return stack.isEmpty(); } } The solution in java8 programming import java.util.*; class Solution{ public static void main(String []argh) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String input=sc.next(); while(input.length() != (input = input.replaceAll("\(\)|\[\]|\{\}", "")).length()); System.out.println(input.isEmpty()); } } } Second solution import java.io.*; import java.math.BigInteger; import java.util.Stack; import java.util.StringTokenizer; public class Solution { StringTokenizer st; BufferedReader in; PrintWriter out; public static void main(String[] args) throws NumberFormatException, IOException { final Solution solver = new Solution(); long time = System.currentTimeMillis(); solver.open(); solver.solve(); //System.out.println("Spend time:" + (System.currentTimeMillis() - time)); solver.close(); } public void open() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); //in = new BufferedReader(new FileReader("input.txt")); //out = new PrintWriter(new FileWriter("output.txt")); } boolean hasMoreTokens() throws IOException { while (st == null || !st.hasMoreTokens()) { String line = in.readLine(); if (line == null) return false; st = new StringTokenizer(line); } return true; } public String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { String line = in.readLine(); if (line == null) return null; st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextToken()); } boolean between(final BigInteger n, long from, long to) { return n.compareTo(BigInteger.valueOf(from)) > -1 && n.compareTo(BigInteger.valueOf(to)) < 1; } boolean check(final String s) { final Stack<Character> stack = new Stack<>(); for (final char c : s.toCharArray()) { switch (c) { case '(': case '{': case '[': stack.push(c); break; case ')': if (stack.isEmpty() || stack.pop() != '(') return false; break; case '}': if (stack.isEmpty() || stack.pop() != '{') return false; break; case ']': if (stack.isEmpty() || stack.pop() != '[') return false; break; } } return stack.isEmpty(); } public void solve() throws NumberFormatException, IOException { String s; while ((s = in.readLine()) != null) { out.println(check(s)); } } public void close() { out.flush(); out.close(); } } coding problems hackerrank solutions java