-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExample26.java
108 lines (85 loc) · 3.5 KB
/
Example26.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package applications.algorithms;
import datastructs.adt.ArrayStack;
/** Category: Algorithms
* ID: Example26
* Description: Balanced brackets
* Taken From: HackerRank Balanced Brackets
* Details:
* A bracket is considered to be any one of the following characters: (, ), {, }, [, or ].
* Two brackets are considered to be a matched pair if the an
* opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type.
* There are three types of matched pairs of brackets: [], {}, and ().
* A matching pair of brackets is not balanced if the set of brackets it encloses are not matched.
* For example, {[(])} is not balanced because the contents in between { and } are not balanced.
* The pair of square brackets encloses a single,
* unbalanced opening bracket, (, and the pair of parentheses encloses a single, unbalanced closing square bracket, ].
* By this logic, we say a sequence of brackets is balanced if the following conditions are met:
*
* It contains no unmatched brackets.
* The subset of brackets enclosed within the confines of a matched pair of brackets is also a matched pair of brackets.
*/
public class Example26 {
public static String matched(String val){
if(val.charAt(0) == '}' || val.charAt(0) == ')' ||
val.charAt(0) == ']'){
return "NO";
}
if(val.charAt(val.length()-1) == '{' || val.charAt(val.length()-1) == '(' ||
val.charAt(val.length()-1) == '['){
return "NO";
}
String result = "YES";
ArrayStack<Character> closing_bracks = new ArrayStack<Character>(val.length());
ArrayStack<Character> stack = new ArrayStack<Character>(val.length());
for(int i=0; i<val.length(); ++i){
stack.push(val.charAt(i));
}
while(stack.empty() != true){
char last = stack.access_top();
if( last == ')' || last == '}' || last == ']') {
stack.pop();
closing_bracks.push(last);
}
else if (last == '(') {
if (closing_bracks.size() != 0 && closing_bracks.access_top() == ')') {
stack.pop();
closing_bracks.pop();
} else {
return "NO";
}
}
else if ( last == '[') {
if (closing_bracks.size() != 0 && closing_bracks.access_top() == ']') {
stack.pop();
closing_bracks.pop();
} else {
return "NO";
}
}
else if(last == '{') {
if (closing_bracks.size() != 0 && closing_bracks.access_top() =='}'){
stack.pop();
closing_bracks.pop();
}
else{
return "NO";
}
}
}
return result;
}
public static void main(String[] args){
String[] strings = { "{[()]}",
"{[(])}", "{{[[(())]]}}",
"[](){()}", "()",
"({}([][]))[]()",
"{)[](}]}]}))}(())(",
"([[)"};
String[] answers = { "YES", "NO", "YES", "YES", "YES",
"YES", "NO", "NO"};
for(int s=0; s<strings.length; ++s){
String answer = Example26.matched(strings[s]);
System.out.println("String "+strings[s]+ " should be "+answers[s]+" and found: "+answer);
}
}
}