-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParenthesisChecker.java
More file actions
executable file
·60 lines (56 loc) · 1.61 KB
/
ParenthesisChecker.java
File metadata and controls
executable file
·60 lines (56 loc) · 1.61 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
57
58
59
60
/**
Answer For
https://practice.geeksforgeeks.org/problems/parenthesis-checker/0#ExpectOP
**/
import java.util.Stack;
import java.util.Scanner;
public class ParenthesisChecker {
public static void main(String[] args){
Scanner inpScanner=new Scanner(System.in);
int testCaseCounter=inpScanner.nextInt();
String openCase="({[";
String closeCase=")}]";
String[] unbalancedString=new String[testCaseCounter];
int count=0;
boolean[] balanced=new boolean[testCaseCounter];
while(count<testCaseCounter){
unbalancedString[count]=inpScanner.next();
int tempCount=count++;
if (unbalancedString[tempCount].length()%2==1){
balanced[tempCount]=false;
continue;
}else {
Stack<Character> temp=new Stack<Character>();
String tempStr=unbalancedString[tempCount];
while (tempStr.length()!=0){
if (openCase.indexOf(tempStr.charAt(0))!=-1){
temp.push(tempStr.charAt(0));
tempStr=tempStr.substring(1);
}else if (closeCase.indexOf(tempStr.charAt(0))!=-1){
if (temp.isEmpty() || temp.peek()!=openCase.charAt(closeCase.indexOf(tempStr.charAt(0)))){
balanced[tempCount]=false;
temp.push('0');
break;
}else{
temp.pop();
tempStr=tempStr.substring(1);
}
}
}
if (temp.isEmpty()){
balanced[tempCount]=true;
}
else{
balanced[tempCount]=false;
}
}
}
for (int i=0;i<testCaseCounter;i++){
if (balanced[i]){
System.out.println("balanced");
}else{
System.out.println("not balanced");
}
}
}
}