-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbracketMatch.java
More file actions
56 lines (45 loc) · 1.13 KB
/
Copy pathbracketMatch.java
File metadata and controls
56 lines (45 loc) · 1.13 KB
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
package StackUsed;
import java.text.BreakIterator;
import Stack.Stack;
import Stack.StackEmptyException;
import Stack.StackSLinked;
public class bracketMatch {
public static boolean bracketMatch(String str) throws StackEmptyException {
Stack s=new StackSLinked();
for(int i=0;i<str.length();i++) {
char c=str.charAt(i);
switch(c) {
case '{':
case '[':
case '(': s.push(Integer.valueOf(c));break;
case '}':
if(!s.isEmpty()&&((Integer)s.pop()).intValue()=='{')
break;
else
return false;
case ']':
if(!s.isEmpty()&&((Integer)s.pop()).intValue()=='[')
break;
else
return false;
case ')':
if(!s.isEmpty()&&((Integer)s.pop()).intValue()=='(')
break;
else
return false;
}
}
if(s.isEmpty()) return true;
else return false;
}
public static void main(String[] args) {
boolean result;
try {
result=bracketMatch("{[(aasd)]}");
System.out.println(result);
} catch (StackEmptyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}