Note: This solution is only for reference purpose. Feel free to use this solution as inspiration and enhance your knowledge but please don't literally copy and paste the code.
Solution of the problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package hackerankjavabasic; import java.util.Scanner; class Parser { boolean isBalanced(String s) { while (s.length() != (s = s.replaceAll("\\(\\)|\\{\\}", "")).length()) ; return s.isEmpty(); } } class Solution { public static void main(String[] args) { Parser parser = new Parser(); Scanner in = new Scanner(System.in); while (in.hasNext()) { System.out.println(parser.isBalanced(in.next())); } in.close(); } } |
Labels : #hackerrank certification ,#java (basic) ,