-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBalancedStringSolution.java
55 lines (48 loc) · 1.34 KB
/
BalancedStringSolution.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
package codilityTest;
import java.util.*;
public class BalancedStringSolution {
public static void main(String[] args) {
String input = "[(])";
char[] in = input.toCharArray();
check(in);
}
public static void check(char[] ch) {
Stack<Character> st = new Stack<>();
char cc;
for (int i = 0; i < ch.length; i++) {
if (ch[i] == '{' || ch[i] == '(' || ch[i] == '[') {
st.push(ch[i]);
}
if (ch[i] == '}' || ch[i] == ')' || ch[i] == ']') {
cc = ch[i];
if (!st.empty()) {
ccsdas(cc, st);
}
}
}
if (st.isEmpty()) {
System.out.println("String is balanced");
} else {
System.out.println("String is not balanced");
}
}
public static void ccsdas(char ch, Stack<Character> s) {
switch (ch) {
case '}':
if (s.peek().equals('{')) {
s.pop();
}
break;
case ']':
if (s.peek().equals('[')) {
s.pop();
}
break;
case ')':
if (s.peek().equals('(')) {
s.pop();
}
break;
}
}
}