-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNesting.java
54 lines (47 loc) · 1.33 KB
/
Nesting.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 lessons.stacksandqueues;
import java.util.Stack;
/**
* Nesting
* https://app.codility.com/programmers/lessons/7-stacks_and_queues/nesting/
* Difficulty : Easy
* Related Topics : Stacks and Queues
*
* created by cenkc on 4/26/2020
*/
public class Nesting {
public static void main(String[] args) {
String A = "())";
Nesting nesting = new Nesting();
System.out.println(nesting.solution(A));
}
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;
return false;
}
public boolean isOpeningChar(char c) {
return c == '(';
}
}