-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBrackets.java
62 lines (55 loc) · 1.79 KB
/
Brackets.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
package lessons.stacksandqueues;
import java.util.Stack;
import static org.junit.Assert.assertEquals;
/**
* Brackets
* https://app.codility.com/programmers/lessons/7-stacks_and_queues/brackets/
* Difficulty : Easy
* Related Topics : Stacks and Queues
*
* created by cenkc on 4/5/2020
*/
public class Brackets {
public static void main(String[] args) {
Brackets brackets = new Brackets();
assertEquals(1, brackets.solution("{[()()]}"));
assertEquals(1, brackets.solution("{}"));
assertEquals(1, brackets.solution("{[]}"));
assertEquals(0, brackets.solution("{[}"));
assertEquals(0, brackets.solution("{{"));
assertEquals(1, brackets.solution("{{}}"));
assertEquals(1, brackets.solution("{{}()[()]}"));
}
public int solution(String S) {
Stack stack = new Stack();
for (int i = 0; i < S.length(); i++) {
char currentChar = S.charAt(i);
if (isOpeningChar(currentChar)) {
stack.push(currentChar);
} else {
if (stack.size() == 0) {
return 0; // first char is closingChar and it's not good
}
if (isPaired((char) stack.peek(), currentChar)) {
stack.pop();
continue;
} else {
return 0;
}
}
}
if (stack.empty()) {
return 1;
}
return 0;
}
private boolean isPaired(char c1, char c2) {
if (c1 == '(' && c2 == ')') return true;
if (c1 == '[' && c2 == ']') return true;
if (c1 == '{' && c2 == '}') return true;
return false;
}
public boolean isOpeningChar(char c) {
return c == '(' || c == '{' || c == '[';
}
}