-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLongestValidParenthesis.java
54 lines (40 loc) · 1.31 KB
/
LongestValidParenthesis.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
import java.lang.*;
import java.io.*;
import java.util.*;
class LongestValidParenthesis {
static int checkSub( char arr[], int start, int end) {
if ( arr[start] == ')')
return 0;
int teller = 0;
int max = 0;
for ( int i = start; i < end; i++) {
if ( arr[i] == '(')
teller++;
else if ( arr[i] == ')')
teller--;
if ( teller == 0 )
max = i;
if ( teller < 0)
return max-(start-1);
}
return max- (start-1);
}
public static void main(String [] args ) {
Scanner sc = new Scanner (System.in);
int t = sc.nextInt();
while ( t-- > 0) {
String s = sc.next();
char [] arr = new char[10001];
ArrayList<Character> sizeAns = new ArrayList<>();
for ( int i = 0; i < s.length(); i++ )
arr[i] = s.charAt(i);
int maxi = 0;
for ( int i = 0; i < s.length()-1; i++ ) {
int temp = checkSub(arr,i,s.length() );
if ( temp > maxi)
maxi = temp;
}
System.out.println(maxi);
}
}
}