-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidParanthesis.java
More file actions
34 lines (34 loc) · 1.03 KB
/
ValidParanthesis.java
File metadata and controls
34 lines (34 loc) · 1.03 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
import java.util.*;
public class ValidParanthesis {
public static boolean validparanthesis(String str){
Stack<Character> s = new Stack<>();
for(int i=0;i<str.length();i++){
char ch = str.charAt(i);
if (ch=='('||ch=='{'||ch=='['){
s.push(ch);
}else{
if (s.isEmpty()){
return false;
}else{
if ((ch==')' && s.peek()=='(') ||
(ch=='}' && s.peek()=='{') ||
(ch==']' && s.peek()=='[')) {
s.pop();
}else{
return false;
}
}
}
}
if (s.isEmpty()){
return true;
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the string: ");
String str = sc.nextLine();
System.out.println(validparanthesis(str));
}
}