-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid.java
More file actions
30 lines (28 loc) · 795 Bytes
/
Copy pathvalid.java
File metadata and controls
30 lines (28 loc) · 795 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
import java.util.Stack;
public class valid {
public static boolean isValid(String str) {
Stack<Character> s = new Stack<>();
for(int i=0; i<str.length(); i++) {
char ch = str.charAt(i);
if(ch == ')') {
int count = 0;
while(s.peek() != '('){
s.pop();
count++;
} if(count <1) {
return true;
} else {
s.pop();
}
} else {
s.push(ch);
}
}
return false;
}
public static void main(String[] args) {
String str = (("a" + "b"));
String str1 = ("a" + "b");
System.out.println(isValid(str1));
}
}