-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrackets.java
More file actions
47 lines (34 loc) · 987 Bytes
/
Copy pathbrackets.java
File metadata and controls
47 lines (34 loc) · 987 Bytes
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
package revision;
import java.util.Stack;
public class brackets {
public static void longest_balancedbracket_substring(String str) {
long mx = 0, c = 0;
Stack<Integer> stk = new Stack<>();
stk.push(-1);
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '(') {
stk.push(i);
}
else {
stk.pop();
if (!stk.isEmpty()) {
long prev = mx;
System.out.println(prev);
mx = Math.max(mx, i - stk.peek());
if (prev == mx) {
c++;
}
} else {
// c++;
stk.push(i);
}
}
}
if (mx > 0) {
System.out.print(mx + " " + c);
} else {
System.out.print("0 1");
}
// Write your solution here
}
}