-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem678.java
More file actions
47 lines (46 loc) · 1.24 KB
/
Problem678.java
File metadata and controls
47 lines (46 loc) · 1.24 KB
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
import java.util.*;
public class Problem678{
// Valid Paranthesis String
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
int t=obj.nextInt();
obj.nextLine();
while(t-->0){
String str=obj.nextLine();
System.out.println(Solution678.valid(str));
}
obj.close();
}
}
class Solution678{
public static boolean valid(String s){
Stack<Integer> open=new Stack<>();
Stack<Integer> star=new Stack<>();
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='('){
open.push(i);
}else if(s.charAt(i)=='*'){
star.push(i);
}else{
if(!(open.isEmpty())){
open.pop();
}else if(!(star.isEmpty())){
star.pop();
}else{
return false;
}
}
}
while(!(open.isEmpty())){
if(star.isEmpty()){
return false;
}else if(open.peek()<star.peek()){
open.pop();
star.pop();
}else{
return false;
}
}
return open.isEmpty();
}
}