-
Notifications
You must be signed in to change notification settings - Fork 858
Expand file tree
/
Copy pathJavaStack.java
More file actions
44 lines (40 loc) · 972 Bytes
/
Copy pathJavaStack.java
File metadata and controls
44 lines (40 loc) · 972 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
/**
*
*/
package com.javaaid.hackerrank.solutions.languages.java.datastructures;
import java.util.Scanner;
import java.util.Stack;
/**
* @author Kanahaiya Gupta
*
*/
public class JavaStack {
private static boolean matchParenthisis(String str) {
Stack<Character> st = new Stack<Character>();
char[] ch = str.toCharArray();
for (char c : ch) {
if (c == '{' || c == '[' || c == '(') {
st.push(c);
} else {
if (c == ']' && !st.isEmpty() && st.pop() == '[') {
continue;
} else if (c == '}' && !st.isEmpty() && st.pop() == '{') {
continue;
} else if (c == ')' && !st.isEmpty() && st.pop() == '(') {
continue;
} else {
return false;
}
}
}
return st.isEmpty();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
String s = in.next();
System.out.println(matchParenthisis(s));
}
in.close();
}
}