-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
41 lines (39 loc) · 965 Bytes
/
App.java
File metadata and controls
41 lines (39 loc) · 965 Bytes
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
public class App
{
public static void main(String[] args) {
Stack<String> stack = new Stack<String>();
String str = "{[()]}";
for (int i = 0; i <str.length();i++){
String s = Character.toString(str.charAt(i));
String rev = reverseBracket(s);
if( isLeftBracket(s))
stack.push(s);
else if(stack.isEmpty() || !stack.pop().equals(rev))
{
System.out.println("invalid");
return;
}
}
System.out.println("valid");
}
public static boolean isLeftBracket(String str)
{
if(str.equals("(") || str.equals("{") || str.equals("[")){
return true;
}
return false;
}
public static String reverseBracket (String str){
if(str.equals("("))
return ")";
if(str.equals(")"))
return "(";
if(str.equals("["))
return "]";
if(str.equals("]"))
return "[";
if(str.equals("}"))
return "{";
return "{";
}
}