-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckBalancedParanthesisString.java
71 lines (64 loc) · 1.89 KB
/
CheckBalancedParanthesisString.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
/* For Example:-
* Input : ({}) , {{([])}}
* Output: true (Balanced)
* Input : {}( , {[(})}
* Output : false (Unbalanced)
* */
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
public class CheckBalancedParanthesisString {
public static void main(String[] args) {
CheckBalancedParanthesisString balance = new CheckBalancedParanthesisString();
Scanner in = new Scanner(System.in);
/*while(in.hasNext()){
String str = in.nextLine();
int startTime = (int)System.currentTimeMillis();
System.out.println("startTime:"+startTime);
boolean result = balance.checkBalanceString(str);
int endTime = (int)System.currentTimeMillis();
System.out.println("endTime:"+ ( endTime - startTime));
int startTime1 = (int)System.currentTimeMillis();
System.out.println("startTime1:"+startTime1);
boolean result1 = balance.checkBalance(str);
int endTime1 = (int)System.currentTimeMillis();
System.out.println("endTime1:"+ (endTime1 - startTime1));
System.out.println(result);
System.out.println(result1);
}
in.close();
}*/
while(in.hasNext()){
String str = in.nextLine();
boolean result = balance.checkBalanceString(str);
System.out.println(result);
}
in.close();
}
public boolean checkBalanceString(String str){
char strArr[] = str.toCharArray();
Deque<Character> stack = new ArrayDeque<Character>();
for(char c : strArr){
char temp = '.';
switch(c){
case '{':
case '(':
case '[': stack.push(c);
break;
case ']': temp = '[';
case ')': if(temp == '.')temp = '(';
case '}': if(temp == '.')temp = '{';
if(stack.isEmpty() || stack.pop() != temp){
return false;
}
break;
}
}
return stack.isEmpty();
}
public boolean checkBalance(String input){
while(input.length() != (input = input.replaceAll("\\(\\)|\\[\\]|\\{\\}", ""))
.length());
return input.isEmpty();
}
}