-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion_Is_Balanced.java
More file actions
44 lines (34 loc) · 899 Bytes
/
Copy pathRecursion_Is_Balanced.java
File metadata and controls
44 lines (34 loc) · 899 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
42
43
44
import java.util.Scanner;
public class Recursion_Is_Balanced {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String str=s.nextLine();
System.out.println(isBalanced(str,true,true,true));
}
private static boolean isBalanced(String str,boolean b1,boolean b2,boolean b3) {
if(str.equals("")) {
if(b1 && b2 && b3) {
return true;
}else {
return false;
}
}
char ch=str.charAt(0);
String ros=str.substring(1);
if(ch=='[') {
return isBalanced(ros, false, b2, b3);
}if(ch=='{') {
return isBalanced(ros, b1, false, b3);
}if(ch=='(') {
return isBalanced(ros, b1, b2, false);
}if(ch==']') {
return isBalanced(ros, true, b2, b3);
}if(ch=='}') {
return isBalanced(ros, b1, true, b3);
}if(ch==')') {
return isBalanced(ros, b1, b2, true);
}else {
return isBalanced(ros, b1, b2, b3);
}
}
}