|
1 |
| -/* |
| 1 | +package data_structures.Stacks; |
2 | 2 |
|
3 |
| -The nested brackets problem is a problem that determines if a sequence of |
4 |
| -brackets are properly nested. A sequence of brackets s is considered properly nested |
5 |
| -if any of the following conditions are true: |
6 |
| - - s is empty |
7 |
| - - s has the form (U) or [U] or {U} where U is a properly nested string |
8 |
| - - s has the form VW where V and W are properly nested strings |
9 |
| -For example, the string "()()[()]" is properly nested but "[(()]" is not. |
10 |
| -The function called is_balanced takes as input a string S which is a sequence of brackets and |
11 |
| -returns true if S is nested and false otherwise. |
12 |
| -
|
13 |
| - author: akshay sharma |
14 |
| - date: 2017-10-17 |
15 |
| -*/ |
16 | 3 | import java.util.Scanner;
|
17 | 4 | import java.util.Stack;
|
18 |
| -import java.util.ArrayList; |
19 | 5 |
|
| 6 | +/** |
| 7 | + * |
| 8 | + * The nested brackets problem is a problem that determines if a sequence of |
| 9 | + * brackets are properly nested. A sequence of brackets s is considered properly |
| 10 | + * nested if any of the following conditions are true: - s is empty - s has the |
| 11 | + * form (U) or [U] or {U} where U is a properly nested string - s has the form |
| 12 | + * VW where V and W are properly nested strings For example, the string |
| 13 | + * "()()[()]" is properly nested but "[(()]" is not. The function called |
| 14 | + * is_balanced takes as input a string S which is a sequence of brackets and |
| 15 | + * returns true if S is nested and false otherwise. |
| 16 | + * |
| 17 | + * @author akshay sharma |
| 18 | + * @date: 2017-10-17 |
| 19 | + * @author <a href="https://github.com/khalil2535">khalil2535<a> |
| 20 | + * |
| 21 | + */ |
20 | 22 | class BalancedBrackets {
|
21 | 23 |
|
22 |
| - static boolean is_balanced(char[] S) { |
23 |
| - Stack<Character> stack = new Stack<>(); |
24 |
| - String pair = ""; |
25 |
| - for (int i = 0; i < S.length; ++i) { |
26 |
| - if (S[i] == '(' || S[i] == '{' || S[i] == '[') { |
27 |
| - stack.push(S[i]); |
28 |
| - } else if (stack.size() > 0) { |
29 |
| - if (!pair.equals("[]") && !pair.equals("()") && !pair.equals("{}")) { |
30 |
| - return false; |
31 |
| - } |
32 |
| - } else { |
33 |
| - return false; |
| 24 | + /** |
| 25 | + * |
| 26 | + * @param s |
| 27 | + * @return |
| 28 | + */ |
| 29 | + static boolean is_balanced(String s) { |
| 30 | + Stack<Character> bracketsStack = new Stack<>(); |
| 31 | + char[] text = s.toCharArray(); |
| 32 | + for (char x : text) { |
| 33 | + switch (x) { |
| 34 | + case '{': |
| 35 | + case '<': |
| 36 | + case '(': |
| 37 | + case '[': |
| 38 | + bracketsStack.push(x); |
| 39 | + break; |
| 40 | + case '}': |
| 41 | + if (bracketsStack.peek() == '{') { |
| 42 | + bracketsStack.pop(); |
| 43 | + break; |
| 44 | + } else { |
| 45 | + return false; |
| 46 | + } |
| 47 | + case '>': |
| 48 | + if (bracketsStack.peek() == '<') { |
| 49 | + bracketsStack.pop(); |
| 50 | + break; |
| 51 | + } else { |
| 52 | + return false; |
| 53 | + } |
| 54 | + case ')': |
| 55 | + if (bracketsStack.peek() == '(') { |
| 56 | + bracketsStack.pop(); |
| 57 | + break; |
| 58 | + } else { |
| 59 | + return false; |
| 60 | + } |
| 61 | + case ']': |
| 62 | + if (bracketsStack.peek() == '[') { |
| 63 | + bracketsStack.pop(); |
| 64 | + break; |
| 65 | + } else { |
| 66 | + return false; |
| 67 | + } |
34 | 68 | }
|
35 | 69 | }
|
36 |
| - |
37 |
| - return stack.isEmpty(); |
38 |
| - } |
39 |
| - |
40 |
| - static void print(Object a) { |
41 |
| - System.out.println(a); |
| 70 | + return bracketsStack.empty(); |
42 | 71 | }
|
43 | 72 |
|
| 73 | + /** |
| 74 | + * |
| 75 | + * @param args |
| 76 | + * @TODO remove main method and Test using JUnit or other methodology |
| 77 | + */ |
44 | 78 | public static void main(String args[]) {
|
45 |
| - try { |
46 |
| - Scanner in = new Scanner(System.in); |
47 |
| - print("Enter sequence of brackets: "); |
48 |
| - String S = in.nextLine(); |
49 |
| - if (is_balanced(S.toCharArray())) { |
50 |
| - print(S + " is balanced"); |
| 79 | + try (Scanner in = new Scanner(System.in)) { |
| 80 | + System.out.println("Enter sequence of brackets: "); |
| 81 | + String s = in.nextLine(); |
| 82 | + if (is_balanced(s)) { |
| 83 | + System.out.println(s + " is balanced"); |
51 | 84 | } else {
|
52 |
| - print(S + " ain't balanced"); |
| 85 | + System.out.println(s + " ain't balanced"); |
53 | 86 | }
|
54 |
| - in.close(); |
55 |
| - } catch (Exception e) { |
56 |
| - e.toString(); |
57 | 87 | }
|
58 | 88 | }
|
59 | 89 | }
|
0 commit comments